5

I'm trying to use a Checkbutton with a function, my_var never changes but it always call my function.

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!

2 Answers 2

9

To make your code work I would use BooleanVar() and the associated get() method to retrieve its value (http://effbot.org/tkinterbook/variable.htm)

For example: (from: 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()
Sign up to request clarification or add additional context in comments.

Comments

0

For me neither BooleanVar() or IntVar() didn't work, so I had to use StringVar() instead.

Here is example code:

check_var = tk.StringVar(value="OFF")
checkbutton = tk.Checkbutton(root, text="Check me!", variable=check_var, onvalue="ON", offvalue="OFF")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.