vendredi 22 juin 2018

Tkinter: checkbox list not responsive

I am trying to create a list of checkbox with a loop. I have modified the implementation of someone else's code which uses the same concept and should have the same function as what I want. When I try to print the state of each checkbox, none of them are updated to 1. They all stay at 0 even if I click them. Here is my code, the test() function doesn't behave as I expect it to. Thanks in advanced for the help

import tkinter
import tkinter.filedialog
import os

# --- functions ---

def browse():

    filez = tkinter.filedialog.askdirectory(parent=window, title='Choose a file')

    ent1.insert(20, filez)

    dirs = os.listdir(filez)

    # remove previous IntVars
    intvar_dict.clear()

    # remove previous Checkboxes
    for cb in checkbutton_list:
        cb.destroy()
    checkbutton_list.clear() 


    for filename in dirs:
        # create IntVar for filename and keep in dictionary
        var = tkinter.IntVar()

        # create Checkbutton for filename and keep on list
        c = tkinter.Checkbutton(window, text=filename, variable=var)
        c.pack()
        intvar_dict[filename] = var
        checkbutton_list.append(c)

def test():


    for key, value in intvar_dict.items():


        if value.get() > 0:
            print("HIIIIII")
        print('selected:', key)
        #print (value.get())

# --- main ---

# to keep all IntVars for all filenames
intvar_dict = {}
 # to keep all Checkbuttons for all filenames
checkbutton_list = []

window = tkinter.Tk()

lbl = tkinter.Label(window, text="Path")
lbl.pack()

ent1 = tkinter.Entry(window)
ent1.pack()

btn1 = tkinter.Button(window, text="Select Path", command=browse)
btn1.pack()

btn1 = tkinter.Button(window, text="Test Checkboxes", command=test)
btn1.pack()

window.mainloop()




Aucun commentaire:

Enregistrer un commentaire