2013-03-02 126 views
0

在编码tkinter复选框时需要一些帮助。 我有一个检查按钮,如果我选择,它将启用许多其他复选框。下面是选择第一个复选框tkinter复选框

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 

后的功能我有两个问题在这里。

  1. 如何删除动态创建的复选框(chk_bx)我的意思是,如果我选择初始复选框它将使许多其他箱,如果我取消第一个复选框它应该删除最初创建的盒子?

  2. 我将如何从动态创建的框中选择“选定/不是”的值?

+1

几个意见:'L11'是'None'因为这是'.grid'返回的内容。您创建Checkbutton时执行Checkbutton命令...您可能希望'lambda name = checkBoxName:box_select(name)' – mgilson 2013-03-02 15:34:19

+0

您不应该使用'var'作为变量名称...也不应该使用全局变量。您可以将该变量传递给该函数。 – 2013-03-02 17:02:52

回答

1

1.如何删除动态创建的复选框?

只是所有checkbuttons添加到列表中,这样在需要的时候你可以叫他们destroy()

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.我将如何得到动态creaed框中的值“选择/不” ?

你必须创建一个Tkinter的IntVar,用于存储取决于checkbutton是否选择不onvalueoffvalue。您还需要保持该对象的轨道,但没有必要建立一个新的列表,因为你可以将它们连接到相应的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 
+0

在def printcheckbuttons()中: 我该如何打印哪个盒子被选中? 为chk_bx在checkbuttons: \t \t如果(chk_bx.var.get()== 1): 我想BR打印checkbutton文本名称 – 2013-03-03 12:15:51

+0

使用['cget'](http://effbot.org/tkinterbook /widget.htm#Tkinter.Widget.cget-method):'print chk_bx.cget('text')' – 2013-03-03 14:50:05

+0

that helps .. thanks – 2013-03-04 07:46:24

相关问题