Gunnm
Gunnm

Reputation: 994

How to set variable in Tkinter in checkbox in newly created window

Everything is good if I'm in root window but I can't set variable in newly created window. It always gives me default empty checkbox. I have one more question and that is how can I save chosen options so when I open options(settings) window again same options will be checked or unchecked.

from Tkinter import *

# TKINTER CODE ######################################################################## #

root = Tk()
root.title('window')
Label(text='input').grid(row=0, sticky=W, padx=10, pady=10)

entry = Entry(root, width=50)
entry.grid(row=1, sticky=W+E, padx=10, pady=10)
entry.insert(0, "input")

text = Text(root, width=80, state=DISABLED)
text.grid(row=2, column=0, sticky=W, padx=0, pady=0)

scrollbar = Scrollbar(root)
scrollbar.grid(row=2, column=1, sticky=N+S+W+E)
scrollbar.config(command=text.yview)

# Settings window


def open_settings():
    settings_win = Toplevel(root)
    settings_win.title('Settings')
    settings_win.geometry('300x300')
    settings_win.resizable(width=FALSE, height=FALSE)
    settings_win_button.config(state='disable')

    def close_settings_window():
        settings_win.destroy()
        settings_win_button.config(state='normal')

    # options to choose from
    option_var1 = IntVar()
    # Make default position to be checked
    option_var1.set(1)

    option_var2 = IntVar()

    option1 = Checkbutton(settings_win, text="option1", variable=option_var1)
    option1.grid(row=0, column=0)

    option2 = Checkbutton(settings_win, text="option2", variable=option_var2)
    option2.grid(row=1, column=0)

    quit_settings_button = Button(settings_win, text='Save', command=close_settings_window)
    quit_settings_button.grid(row=10, column=0)

    settings_win.protocol("WM_DELETE_WINDOW", close_settings_window)

settings_win_button = Button(root, text='Options', command=open_settings)
settings_win_button.grid(row=0, column=0)

# Menu Bar ############################################################################ #
menubar = Menu(root)

# create a pulldown menu, and add it to the menu bar
filemenu = Menu(menubar, tearoff=0)
# filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

helpmenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Help", menu=helpmenu)

# display the menu
root.config(menu=menubar)

# End ################################################################################# #

root.mainloop()

I want option1 in newly created window to be checked.

Upvotes: 1

Views: 662

Answers (2)

Tadhg McDonald-Jensen
Tadhg McDonald-Jensen

Reputation: 21453

the reason option1 starts unchecked despite setting the variable to 1 is because option_var1 is a local variable to the open_settings function, once the function finishes option_var1 is deleted and the check boxes lose their reference. You can fix this simply by adding a global statement:

def open_settings():
    global option_var1,option_var2
    ....

Although any case that the option_var1 keeps a reference after creation (which is needed to do anything with the settings anyway) will also fix the reference issue.

Upvotes: 1

maccartm
maccartm

Reputation: 2115

Try add the line root.wait_window(settings_win) after your settings_win.protocol... line:

settings_win.protocol("WM_DELETE_WINDOW", close_settings_window)
root.wait_window(settings_win)

I think what's happening is the running event loop is still trying to process the root window, so it is not actively updating the Toplevel. What wait_window will do is create a local event loop. The root will now wait for the Toplevel to be destroyed to continue processing. This forces the Toplevel to constantly update, which means the default value of option1 will be checked.

You will need to store the value of option_var1 and option_var2 somewhere, however. Currently, they are recreated every time you open the option window, which means they always take their default values.

Hopefully this helps you solve your problem.

Upvotes: 1

Related Questions