Search code examples
pythonfor-looptkintertkinter.checkbutton

Python Tkinter for loop checkbuttons not working as intended


having trouble with tkinter and making checkbuttons through a for loop. i need to create a dynamic amount of buttons based on a previously created list.

With this code, all of the buttons get ticked and unticked at the same time when i click one of them. also, calling checktab[i] doesn't actually give me the value the corresponding button is set to, the value stays the same whether the button is ticked or not.

checktab = []
Button = []
for i in range(len(repo_list)):
    checktab.append(0)
    Button.append(tkinter.Checkbutton(window, text = repo_list[i]["repo_name"], variable = checktab[i], onvalue = 1, offvalue = 0))
    Button[i].pack()

Solution

  • Tkinter uses IntVars to keep track of the value of a checkbutton. You can't just use a normal integer. Changing

    checktab.append(0)
    

    to

    checktab.append(tkinter.IntVar())
    

    should work. You can then use checktab[i].get() to get the value of the IntVar.