lundi 2 mars 2020

How to make a tkinter pop up window with buttons, which will wait for the user to click?

I'm making program which will sort and rename files. If the file is .jpg I want to ask if it belongs to "A" or "B". My problem is that when in files are more than one dir, program will make for every one choose = Toplevel() in the same moment (not the same moment but will make one, then loop in for file in files: and make another one ,and so on, without waiting for Buttons). Then choosing A or B will only affect the last loop in for file in files:. Is it possible to stop program and looping until user decide to click A or B ?

Here is a piece of code I'm talking about:

for file in files:
cwd = os.getcwd()
shutil.copy(file, cwd)
dir_file = cwd + '/' + os.path.basename(file)
file_name, file_ext = os.path.splitext(os.path.basename(file))
if file_ext.upper() in [".JPG", ".JPEG", ".PDF", ".TIF"]:

    def send_a(a):
        adress = dir_file
        name = "photo_A0" + str(a) + file_ext
        name = cwd + "/" + name
        os.rename(adress, name)
        a += 1
        return a

    def send_b(b):
        adress = dir_file
        name = "photo_B0" + str(b) + file_ext
        name = cwd + "/" + name
        os.rename(adress, name)
        b += 1
        return b


    choose = Toplevel()
    choose_one = Label(choose, text="Choose one: " + os.path.basename(file), width=50)
    choose_one.grid(row=0, column=0)

    guzik3 = Button(choose, text="A", command=lambda: [send_a(a), choose.destroy()])
    guzik3.grid(row=0, column=3, padx=10, pady=6)
    guzik4 = Button(choose, text="B", command=lambda: [send_b(b), choose.destroy()])
    guzik4.grid(row=0, column=3, padx=10, pady=6)
else ...

Aucun commentaire:

Enregistrer un commentaire