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()