Reputation: 605
need some help in coding tkinter checkbox. i am having a check button which if i select, it will enable many other check box. below is the function after selecting the first check box
def enable_():
# Global variables
global var
# If Single test
if (var.get()==1):
Label (text='Select The Test To Be Executed').grid(row=7,column=1)
L11 = Label ().grid(row=9,column=1)
row_me =9
col_me =0
test_name_backup = test_name
for checkBoxName in test_name:
row_me = row_me+1
chk_bx = Checkbutton(root, text=checkBoxName, variable =checkBoxName, \
onvalue = 1, offvalue = 0, height=1, command=box_select(checkBoxName), \
width = 20)
chk_bx.grid(row = row_me, column = col_me)
if (row_me == 20):
row_me = 9
col_me = col_me+1
i have two question here.
How to delete the dynamically created check boxes (chk_bx ) i mean if i select the initial check box it will enable many other boxes , if i deselect the first check box it should remove the initially created boxes?
How will i get the value from the dynamically creaed box "selected / not"?
Upvotes: 0
Views: 3274
Reputation: 20679
1. How to delete the dynamically created check boxes?
Just add all your checkbuttons to a list, so you can call destroy()
on them when needed:
def remove_checkbuttons():
# Remove the checkbuttons you want
for chk_bx in checkbuttons:
chk_bx.destroy()
def create_checkbutton(name):
return Checkbutton(root, text=name, command=lambda: box_select(name),
onvalue=1, offvalue=0, height=1, width=20)
#...
checkbuttons = [create_checkbutton(name) for name in test_name]
2. How will i get the value from the dynamically creaed box "selected / not"?
You have to create a Tkinter IntVar
, which is used to store the onvalue
or offvalue
depending on whether the checkbutton is selected or not. You also need to keep track of this object, but it isn't necessary to create a new list, since you can attach them to the corresponding checkbutton:
def printcheckbuttons():
for chk_bx in checkbuttons:
print chk_bx.var.get()
def create_checkbutton(name):
var = IntVar()
cb = Checkbutton(root, variable=var, ...)
cb.var = var
return cb
Upvotes: 1