Home>

I am a beginner just starting programming.
I'm sorry for the first question, so there are a lot of things that are difficult or difficult to communicate ...

I want to switch the dir_path part with a button created with tkinter.

Applicable source code
def Test ():
    directory = Path.home ()/dir_path
    file_type = ('png',)
    for type in file_type:
        (Path (directory)/type) .mkdir (exist_ok = True)
        p = Path (directory) .glob ('*.' + type)
        for f in p:
            f.rename (Path (directory) /type/f.name)
Tried I tried to callx = 1 to 0 with A associated with the button.
When executed, the process is executed in Documents called in 1.

Excerpt
import tkinter
from pathlib import Path
~
#Set button 123
~
dir_test = ['Downloads', 'Documents']
x = 1
dir_path = dir_test [x]
def A ():
    global x
    x = 0
def B ():
    global x
    x = 1
def Test ():
    global dir_path
    directory = Path.home ()/dir_path
    file_type = ('png',)
    for type in file_type:
        (Path (directory)/type) .mkdir (exist_ok = True)
        p = Path (directory) .glob ('*.' + type)
        for f in p:
            f.rename (Path (directory) /type/f.name)
button1 ['command'] = A
button2 ['command'] = B
button3 ['command'] = Test
root.mainloop ()
Supplemental information (FW/tool version etc.)

Python3.7

  • Answer # 1

    Since

    dir_pathhas been initialized withdir_test [1], it may not be reflected even if x is changed after that.

    Does it work by referring directly todir_test [x]as shown below?

    import tkinter
    from pathlib import Path
    ~
    #Set button 123
    ~
    dir_test = ['Downloads', 'Documents']
    x = 1
    def A ():
        global x
        x = 0
    def B ():
        global x
        x = 1
    def Test ():
        global dir_test
        global x
        directory = Path.home ()/dir_test [x]
        file_type = ('png',)
        for type in file_type:
            (Path (directory)/type) .mkdir (exist_ok = True)
            p = Path (directory) .glob ('*.' + type)
            for f in p:
                f.rename (Path (directory) /type/f.name)
    button1 ['command'] = A
    button2 ['command'] = B
    button3 ['command'] = Test
    root.mainloop ()

  • Answer # 2

      

    directory = Path.home ()/dir_path

    What do you expect from

    ?