简体   繁体   English

如何在Python的Tkinter中获取多个Checkbutton的文本值列表?

[英]How to get the list of text values of multiple Checkbuttons in Tkinter in Python?

Using following code I try to get updated list of checkbuttons' corresponding text values, everytime checkbutton is checked or unchecked: 使用以下代码,每当选中或取消选中checkbutton时,我都会尝试获取checkbutton的相应文本值的更新列表:

import Tkinter as tk

opt = []
def chkbox_checked():
    for ix, item in enumerate(cb):
        opt.append(cb_v[ix].get())
    print opt
root = tk.Tk()
mylist = [
'NR',
'ECEF X',
'ECEF Y',
'ECEF Z',
'height'
]
cb = []
cb_v = []
for ix, text in enumerate(mylist):
    cb_v.append(tk.StringVar())
    cb.append(tk.Checkbutton(root, text=text, onvalue=text, variable=cb_v[ix],   comand=chkbox_checked))
    cb[ix].grid(row=ix, column=0, sticky='w')   
label = tk.Label(root, width=20)
label.grid(row=ix+1, column=0, sticky='w')
root.mainloop()

If for example all buttons are checked from the first to the last, my desired output would be: 例如,如果从头到尾都选中了所有按钮,则我想要的输出将是:

['NR']
['NR','ECEF X]
['NR','ECEF X','ECEF Y']
['NR','ECEF X','ECEF Y','ECEF Z]
['NR','ECEF X','ECEF Y','ECEF Z','height',]

but with above code I get multiplied output and also there's something wrong with checkbuttons themselves, their state is checked from the beginning. 但是通过上面的代码,我得到了成倍的输出,并且checkbutton本身也出了问题,它们的状态从一开始就被检查了。 Any help would be appreciated. 任何帮助,将不胜感激。

One problem with the above is the opt.append in chkbox_checked ... Since this function gets called everytime a button is checked/unchecked, the length of the opt list will increase by the number of checkbuttons you have everytime one of the buttons is clicked. 上面的一个问题是chkbox_checked中的opt.append ... ...由于每次选中/取消选中按钮时都会调用此函数,因此,每次单击其中一个按钮时,opt列表的长度将增加您拥有的checkbutton的数量。 。 The solution (posted below) is to initialize opt when you create the buttons and then just update it's elements in chkbox_checked. 解决方案(在下面发布)是在创建按钮时初始化opt,然后仅在chkbox_checked中更新其元素。 As far as the state of the buttons on creation, I'm not sure why they're initially checked, but you can easily deselect the buttons at initialization as well using the deselect method. 至于创建时按钮的状态,我不确定为什么要首先检查它们,但是您也可以使用deselect方法在初始化时轻松地取消选择按钮。

import Tkinter as tk

opt = []
def chkbox_checked():
    for ix, item in enumerate(cb):
        opt[ix]=(cb_v[ix].get())
    print opt
root = tk.Tk()  
mylist = [
'NR',
'ECEF X',
'ECEF Y',
'ECEF Z',
'height' 
]
cb = []
cb_v = []
for ix, text in enumerate(mylist):
    cb_v.append(tk.StringVar())
    off_value=0  #whatever you want it to be when the checkbutton is off
    cb.append(tk.Checkbutton(root, text=text, onvalue=text,offvalue=off_value,
                             variable=cb_v[ix],
                             command=chkbox_checked))
    cb[ix].grid(row=ix, column=0, sticky='w')
    opt.append(off_value)
    cb[-1].deselect() #uncheck the boxes initially.
label = tk.Label(root, width=20)
label.grid(row=ix+1, column=0, sticky='w')
root.mainloop()

Another trick that may be useful is instead of keeping 2 lists (cb and cb_v), you could just add the StringVars as attributes to your checkbuttons. 另一个有用的技巧是代替保留2个列表(cb和cb_v),而只需将StringVars作为属性添加到您的检查按钮即可。 eg: 例如:

v=tk.StringVar()
cb.append(tk.CheckButton(... , variable=v, ...)
cb[-1].v=v

Then you just have one list with all the data. 然后,您只有一个包含所有数据的列表。 The corresponding chkbox_checked would look like: 相应的chkbox_checked如下所示:

def chkbox_checked():
   opt=[chkbox.v.get() for chkbox in cb]
   print opt

(Note this also eliminates the need for a global opt list ... although there are probably a whole bunch of other ways to get rid of that list) (请注意,这也消除了对全局opt列表的需求……尽管可能还有很多其他方法可以摆脱该列表)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM