1

I am using tkinter in python 3. I have a checkbutton and button on my GUI:

entercheck = Checkbutton(window1, variable = value)
entercheck.pack()
savebutton = Button(window1, width=5, height=2, command = savecheck)
savebutton.pack()

where value=IntVar(). I am trying to make it so that when clicked, the button saves the status of the checkbutton to the variable status. I have tried:

def savecheck():
    global status
    status = value.get()

However, this always results in status (which is a global variable) being equal to 0 no matter whether the checkbutton is checked or not. Why is this?

I have had a look at this question: Getting Tkinter Check Box State and this method seems to be working for them?

Edit:

I created a smaller version of my program to try and get it to work, this time just trying to output the value of the checkbutton variable, but it still doesn't work. Here is the whole code:

from tkinter import *
root=Tk()

def pressbttn1():

    def savecheck():
        print (value.get()) #outputs 0 no matter whether checked or not???

    window1 = Tk() 

    value=IntVar()
    entercheck = Checkbutton(window1, bg="white", variable = value)
    entercheck.pack()

    savebttn = Button(window1,text= "Save", command = savecheck)
    savebttn.pack()

class Application(Frame):

    def __init__(self, master):
        super(Application, self).__init__(master)
        self.grid()
        self.create_widgits()

    def create_widgits(self):

        self.bttn1 = Button(self, text= "New Window", command = pressbttn1)
        self.bttn1.pack()

#main
app=Application(root)
root.mainloop()

I don't understand why the above code doesn't work, when the below does:

from tkinter import *
master = Tk()

def var_states():
   print(check.get())

check = IntVar()
Checkbutton(master, text="competition", variable=check).pack()

Button(master, text='Show', command=var_states).pack()
mainloop()
Community
  • 1
  • 1
Oceanescence
  • 1,967
  • 3
  • 18
  • 28
  • I am unable to reproduce your problem using `root` instead of `self`, were `root = Tkinter.Tk()` - so your problem may be because `entercheck` and `savebutton` are local variables. Try making them globals or storing them as attributes of `self`: i.e. `self.entercheck = Checkbutton(...)` and `self.savebutton = Button(...)`. – martineau Feb 06 '15 at 19:53
  • Oops, sorry I typed self by mistakes here - in my program it's window1, as there are multiple windows. – Oceanescence Feb 06 '15 at 20:15
  • Oh no, could the error be something to do with the fact that it's in a different window to the main one? – Oceanescence Feb 06 '15 at 20:16
  • I suggest trying to create a short self-contained illustration of the problem ([SSCCE](http://sscce.org)) -- it may even help you solve it yourself. I mentioned saving widgets as globals or attributes because not doing one or the other is often a problem when dealing with Tkinter. Local variables cease to exist after the function or method they are in returns which can cause problems if you're told Tkinter to use them. – martineau Feb 06 '15 at 21:15
  • Nice question update. I'm looking at it...but no clue so far – thinking maybe it's a `tkinter` bug. – martineau Feb 07 '15 at 18:23
  • @Bryan Oakley might be able to help. – martineau Feb 07 '15 at 18:31

2 Answers2

2

status is a variable local to the savecheck function. Make it a global and it will work as intended.

status = 0
value = IntVar()

def savecheck():
    global status
    status = value.get()

entercheck = Checkbutton(self, variable = value)
entercheck.pack()
savebutton = Button(self, width=5, height=2, command = savecheck)
savebutton.pack()
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
1

In all honesty, I'm not entirely clear about why your program doesn't work, but nevertheless have a solution for you.

In your pressbttn1() function, change the:

    window1 = Tk()

to

    window1 = Toplevel()

Calling Tk() creates a new root window. Calling Toplevel() creates a separate top-level window widget which exists independently from the root window, but is controlled by the same window manager. An application can have any number of them.

See Toplevel Window Methods for additional information about window managers.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • You really have been a life saver, thank you very much! – Oceanescence Feb 07 '15 at 20:53
  • You're welcome. We're not supposed to chat here...but I got _really_ curious about what was going on after I tried correcting absolutely everything I could think of that might be cause looking for an explanation – so I guess I was doing it for both of us. – martineau Feb 07 '15 at 21:00