0

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

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
David
  • 481
  • 3
  • 13
  • Can you turn this into a [mcve]? If I just take the posted code, add the line `create_simulation_videos()` at the bottom, and run it, everything works—and it alternates between printing `False` and `True`. – abarnert May 22 '18 at 01:21
  • Cannot confirm your problem. Your code works fine for me. – DYZ May 22 '18 at 01:22
  • 1
    You are calling `Tk()` twice - once in `create_simulation_videos()`, and once previously in the process of creating the Button that invokes that function. *That inevitably screws everything up* - use `Toplevel()` to create additional windows. – jasonharper May 22 '18 at 02:39
  • Since you couldn't confirm my problem I updated my question and wrote full code that you can actually run and see the problem I'm talking about. please look at it again. thanks. – David May 22 '18 at 13:45
  • Anybody knows? I still couldn't find out how to make it work. – David Jun 05 '18 at 23:13

0 Answers0