简体   繁体   English

从动态检查按钮中检索值

[英]Retrieving values from dynamic checkbuttons

I am using a for loop to create checkbuttons based off a list.我正在使用 for 循环根据列表创建检查按钮。 I then want a button that'll "get" which buttons have been checked or not.然后我想要一个按钮,它可以“获取”哪些按钮已被选中。

Since I am not manually creating the checkbuttons, I am not naming the variables so how can I.get() whether they are on or off?由于我没有手动创建检查按钮,因此我没有命名变量,所以 I.get() 是打开还是关闭?

Thanks very much this worked nicely非常感谢这很好用

''' from tkinter import * ''' 从 tkinter 导入 *

root = Tk()根 = Tk()

shopping = ['Apples','Pears','Bananas']购物= ['苹果','梨','香蕉']

chbuttonlist = []按钮列表 = []

for item in shopping:对于购物项目:

var = StringVar()
b = Checkbutton(root,text=item,variable=var)
b.deselect()
b.pack()
chbuttonlist.append(var)

def printlist():定义打印列表():

lst=[]
for var in chbuttonlist:
    lst.append(var.get())

for i in range(len(lst)):
    if lst[i]=='1':
        print(shopping[i])

Button(root,text='Click to print checked items',command=printlist).pack() Button(root,text='点击打印选中项',command=printlist).pack()

root.mainloop() root.mainloop()

''' '''

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

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