1

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()
Zefile
  • 13
  • 3
  • The value of the `variable=` option for Checkbuttons and similar widgets has to be one of Tkinter's Var types - `IntVar`, `StringVar`, etc. A plain `0` is useless here - you're telling all of the widgets to store their state in a variable *named* `0`, so they all necessarily share the same state. – jasonharper Nov 10 '22 at 16:56

1 Answers1

2

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.

Henry
  • 3,472
  • 2
  • 12
  • 36