【问题标题】:Python tkinter check button printing PY_VAR0Python tkinter 检查按钮打印 PY_VAR0
【发布时间】:2015-09-16 13:50:55
【问题描述】:

这就是代码,很大程度上来自这个问题:

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”,如何获得这些值?

【问题讨论】:

    标签: python user-interface python-3.x tkinter


    【解决方案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_VAR0PY_VAR1,因为您将值设置为带有 enable[machine] = Variable() 的值,这会覆盖您的 dict 中的值,因此您得到的输出是有意义的。

    【讨论】:

    • 别担心,愉快的 tkintering!
    猜你喜欢
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    相关资源
    最近更新 更多