1

Hi im trying to write a piece of code on python that lists a value in a label depending how many checkbuttons are ticked. why isnt my code working?

var1=IntVar()
var2=IntVar()
var3=IntVar()



inlabel = StringVar()


label =  Label (the_window, height = 1,bg = "white",width = 30, textvariable = inlabel,font = ("arial",50,"normal")).pack()
def check():
    if(var1 == 0 and var2 == 0 and var3==1):
        inlabel.set("odd")
    if(var1 == 0 and var2 == 1 and var3==1):
        inlabel.set("even")
    if(var1 == 1 and var2 == 1 and var3==1):
        inlabel.set("odd")
    if(var1 == 0 and var2 == 1 and var3==0):
        inlabel.set("odd")
    if(var1 == 1 and var2 == 1 and var3==0):
        inlabel.set("even")
    if(var1 == 0 and var2 == 0 and var3==0):
        inlabel.set("null")
    if(var1 == 1 and var2 == 0 and var3==0):
        inlabel.set("odd")
    if(var1 == 1 and var2 == 0 and var3==1):
        inlabel.set("even")

check1 = Checkbutton(the_window, text= "Gamma",variable=var1,command=check)
check2 = Checkbutton(the_window, text= "Beta",variable=var2,command=check)
check3 = Checkbutton(the_window, text= "Alpha",variable=var3,command=check)
check1.pack(side=RIGHT)
check2.pack()
check3.pack(side=LEFT)

Thanks :)

3
  • Presumably you are using tkinter? If so, it would mentioning that in the question title and tags (otherwise it's not clear where Checkbutton etc are defined) Commented May 4, 2014 at 6:12
  • possible duplicate of stackoverflow.com/questions/23425467/… Commented May 4, 2014 at 7:36
  • haha yep looks like it Commented May 5, 2014 at 8:07

2 Answers 2

2

You need to use get and assign it to another variable instead of var1. So something like this should work.

value1 = var1.get()
value2 = var2.get()
value3 = var3.get()

I assumed you defined your parent window (the_window) in your actual program.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks and yes I did define the_window earlier :)
2

You need to use the var.get(). Here's a working example in Python3.3.

from tkinter import *

root=Tk()

class CheckB():
    def __init__(self, master, text):
        self.var = IntVar()
        self.text=text
        c = Checkbutton(
            master, text=text,
            variable=self.var,
            command=self.check)
        c.pack()

    def check(self):
        print (self.text, "is", self.var.get())


check1 = CheckB(root, text="Gamma")
check2 = CheckB(root, text="Beta")
check3 = CheckB(root, text="Alpha")

Hope that helped! - Ed

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.