0

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.

  1. 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?

  2. How will i get the value from the dynamically creaed box "selected / not"?

2
  • 2
    A few observations: L11 is None since that is what .grid returns. Your Checkbutton command is executed when you create the Checkbutton ... You probably want lambda name=checkBoxName:box_select(name) Commented Mar 2, 2013 at 15:34
  • you shouldn't use var as a variable name... Nor should you use global variables. You can just pass the variable into the function. Commented Mar 2, 2013 at 17:02

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

in the def printcheckbuttons(): how do i print which box is selected ? for chk_bx in checkbuttons: if(chk_bx.var.get()==1): i want the checkbutton text name to br printed
Use cget: print chk_bx.cget('text')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.