#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0

    Tkinter-Checkbutton's variable doesn't change


    Hi everyone..

    I have a problem coding in python tkinter.The variable of the checkbutton returns ( is) always 0.So I can't know if the user selects each checkbutton or not.
    My code is below:

    Code:
    from Tkinter import *
    
    
    class First_Opening():
        
        def __init__(self):
            
         
         self.first_open_var1=IntVar()
         self.first_open_var2=IntVar()
         self.list12=[]
         top=Tk()  
         top.title('Program')
         top.resizable(width=FALSE, height=FALSE)
         top.geometry('200x60+200+200')
         top.configure(background='light blue')                                         
         top.option_add("*background", "light blue")
    
         self.first_open_label6=Label(top,text='Your Choise:')
         self.first_open_label6.place(x=10,y=25)
         self.checkBox1=Checkbutton(top,text='First Choise', command=self.first_open_first, variable=self.first_open_var1)
         self.checkBox1.place(x=90,y=10)
         self.checkBox2=Checkbutton(top,text='Second Choise', command=self.first_open_second, variable=self.first_open_var2)
         self.checkBox2.place(x=90,y=30)
    
    
    
        def first_open_first(self):
           # print "1o",self.first_open_var2.get() 
            if self.first_open_var1.get()==1:
              self.list12.append("First Choice")
             # print self.list12
              self.checkBox2.deselect()
                    
        def first_open_second(self):
            #print "2o",self.first_open_var2.get()
            if self.first_open_var2.get()==1:
              self.list12.append("Second Choise")
             # print self.list12
              self.checkBox1.deselect()
    
    
    form1=Tk()
    form1.title=('Something')
    form1.resizable(width=FALSE, height=FALSE)
    form1.geometry('250x130+200+200')
    form1.configure(background='light blue')                                        
    form1.option_add("*background", "light blue")
    
    dial_first_opening = First_Opening()
    form1.destroy()
    
    form1.mainloop()
    What do you think is the problem?

    Thanks in advance.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,891
    Rep Power
    481
    Code:
    from Tkinter import *
    
    class First_Opening():
        
        def __init__(self):
         top=Tk()  
         self.first_open_var1=IntVar(top)     # master is a top level that isn't destroyed
         self.first_open_var2=IntVar(top)
         self.list12=[]
         top.title('Program')
         top.resizable(width=FALSE, height=FALSE)
         top.geometry('200x60+200+200')
         top.configure(background='light blue')                                         
         top.option_add("*background", "light blue")
    
         self.first_open_label6=Label(top,text='Your Choise:')
         self.first_open_label6.place(x=10,y=25)
         self.checkBox1=Checkbutton(top,text='First Choise', command=self.first_open_first, variable=self.first_open_var1)
         self.checkBox1.place(x=90,y=10)
         self.checkBox2=Checkbutton(top,text='Second Choise', command=self.first_open_second, variable=self.first_open_var2)
         self.checkBox2.place(x=90,y=30)
         top.mainloop()                       #**************** run mainloop of top
    
        def first_open_first(self):
            print "1o",self.first_open_var1.get()   ###### changed to var1
            if self.first_open_var1.get()==1:
              self.list12.append("First Choice")
              print self.list12
              self.checkBox2.deselect()
                    
        def first_open_second(self):
            print "2o",self.first_open_var2.get()
            if self.first_open_var2.get()==1:
              self.list12.append("Second Choise")
              print self.list12
              self.checkBox1.deselect()
    
    ################if you had needed that other top level you could withdraw rather than destroy it.
    
    dial_first_opening = First_Opening()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo