简体   繁体   English

Tkinter Checkbutton不会更改我的变量

[英]Tkinter Checkbutton doesn't change my variable

I'm trying to use a Checkbutton with a function, my_var never changes but it always call my function. 我正在尝试使用带有函数的Checkbutton,my_var永远不会改变,但它总是调用我的函数。

here the code: 这里的代码:

my_var = False
def controllo_carta():
    global my_var
    print str(my_var)

[...]

c = tk.Checkbutton(toolbar, text="press me",onvalue=True,offvalue=False,variable=my_var,command=controllo_carta)
c.select()
c.pack(side=tk.LEFT,padx=2,pady=2)

print 'my var:' + str(my_var)

[...]

where is my mistake? 我的错在哪里?

thanks! 谢谢!

To make your code work I would use BooleanVar() and the associated get() method to retrieve its value ( http://effbot.org/tkinterbook/variable.htm ) 为了使你的代码工作,我将使用BooleanVar()和相关的get()方法来检索它的值( http://effbot.org/tkinterbook/variable.htm

For example: (from: http://effbot.org/tkinterbook/checkbutton.htm ) 例如:(来自: http//effbot.org/tkinterbook/checkbutton.htm

from Tkinter import *

master = Tk()

var = BooleanVar()

def cb():
    print "variable is {0}".format(var.get())

c = Checkbutton(master, text="Press me", variable=var, command=cb)
c.pack()

mainloop()

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

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