I'm a having a problem that I can't check if the checkbutton in tkinter is checked or not (I always get False). I'm sure I'm missing something but I've tried for so long and I don't know what I'm doing wrong. Bellow is the minimum code I could write that shows the problem:
There are two files:
The main file (that you run) is called "main_GUI.py" and here's the code:
from tkinter import *
import sub_GUI
root = Tk()
button1 = Button(root, bg='white', text="Click me", font=("Helvetica",12),
height=3, width=25, command=sub_GUI.create_new_window)
button1.grid(column=1, row=1)
root.mainloop()
The second file is called "sub_GUI.py" and here's the code:
from tkinter import *
var1 = None
sub_root = None
def create_widgets():
global sub_root
global var1
var1 = BooleanVar()
Checkbutton(sub_root,
text="A",
variable=var1,
command=do_something
).grid(row=2, column=0, sticky=W)
def do_something():
global var1
is_current_joint_checked = var1.get()
if is_current_joint_checked:
print('True')
else:
print('False')
def create_new_window():
global sub_root
sub_root = Tk()
sub_root.title("Movie Chooser")
create_widgets()
The problem is that in the window that appears with the letter 'A' (after you clicked the button), every time I check/uncheck 'A' it prints False (never prints True).
Can anyone tell me what's the problem?
Thanks