clarencess3
clarencess3

Reputation: 15

Problem: Can only check multiple checkbuttons together?

I would like to generate checkbuttons for multiple items. Due to the repetition, I used a loop to initialize the widget and the default state, saving the BooleanVar() and widgets in separate lists. But by doing so, I can only check either check or uncheck all of them together.

I already tried to set different value to the BooleanVar in the list from within the loop, but to no avail.

ckbtnVarList = [tk.BooleanVar()]*len(ckbtnDict["Tag"])
ckbtnWdtList = [None]*len(ckbtnDict["Tag"])

for idx in range(len(ckbtnDict["Tag"])):
    ckbtnVarList[idx].set(1)
    ckbtnWdtList[idx]=ttk.Checkbutton(mainfrm, text=ckbtnDict["Text"][idx], variable=ckbtnVarList[idx]).pack()

Upvotes: 0

Views: 86

Answers (1)

scotty3785
scotty3785

Reputation: 7006

As specified in the comments above, you need to create your list of BooleanVar's with a list comprehension or a list. The below code shows how to do this. Since you didn't provide a complete code example, I've had to make some assumptions about your input data.

import tkinter as tk

ckbtnDict = {}
ckbtnDict['Tag'] = ["Tag1","Tag2","Tag3"]
ckbtnDict["Text"] = ["Txt1","Txt2","Txt3"]

mainfrm = tk.Tk()
ckbtnVarList = [tk.BooleanVar() for i in range(len(ckbtnDict["Tag"]))]
ckbtnWdtList = [None for i in range(len(ckbtnDict["Tag"]))]

for idx in range(len(ckbtnDict["Tag"])):
    ckbtnVarList[idx].set(1)
    ckbtnWdtList[idx]=tk.Checkbutton(mainfrm, text=ckbtnDict["Text"][idx], variable=ckbtnVarList[idx])
    ckbrnWdtList[idx].pack()

mainfrm.mainloop()

Upvotes: 1

Related Questions