0

I'm struggling with a simple tkinter issue, I want to check the output from checkbuttons in a loop. But the result is always 0. What do I do wrong?

from tkinter import *

root = Tk()

files = ["amsterdam", "rotterdam", "groningen"]
rownr = 0

filesdict = dict.fromkeys(files, "")

for city in files:
    rownr = rownr + 1
    filesdict[city] = IntVar()
    Checkbutton(root, text=city, variable=filesdict[city]).grid(row=rownr)
    
exitbutton = Button(root, text="exit", command=root.quit)
exitbutton.grid(row=5)

print (filesdict['amsterdam'].get())    
root.mainloop()
Arie Osdorp
  • 642
  • 2
  • 8
  • 17
  • 1
    You're printing the value _before_ launching the GUI, i.e. before the user has had the possibility to set any checkbuttons. Try `print (filesdict['amsterdam'].get())` _after_ exiting the GUI, you will see the correct value – gimix Jul 02 '21 at 15:43
  • Thank you! I've wasted a couple of hours with this silly problem! – Arie Osdorp Jul 02 '21 at 15:53

0 Answers0