所以这是代码,它大量吸取了这个问题:
How do I create multiple checkboxes from a list in a for loop in python tkinter
from tkinter import *
root = Tk()
enable = {'jack': 0, 'john': 1}
def actright(x):
print(enable.get(x))
for machine in enable:
enable[machine] = Variable()
l = Checkbutton(root, text=machine, variable=enable[machine],
command= actright(machine))
l.pack(anchor = W)
root.mainloop()
我希望输出为:
0
1
而是输出:
PY_VAR0
PY_VAR1
如何在数字前面没有“PY_VAR”的情况下获取这些值?
答案 0 :(得分:1)
删除enable[machine] = Variable()
for machine in enable:
l = Checkbutton(root, text=machine, variable=enable[machine],
command= actright(machine))
l.pack(anchor = W)
root.mainloop()
您看到PY_VAR0
和PY_VAR1
,因为您将值设置为enable[machine] = Variable()
的值,这会覆盖您的dict中的值,因此您可以获得所做的输出。< / p>