Dominik Praski Dominik Praski - 2 days ago 5
Python Question

How to get the tick boxes to change value in the second window?

I got example code were there are two windows and in the second one there's a tick box that doesn't change value when it is ticked. How can I fix this? I tried returning the value of the tickbox however that failed as well.

from tkinter import *

root = Tk()

def open_custom_gui():
custom_gui()

b = Button(root,command=open_custom_gui)
b.grid(row=1,column=0)

def custom_gui():
def getinfo():
print(var1.get())

custom= Tk()
var1 = IntVar()
tickbox_1 = Checkbutton(custom,text='TEST',variable=var1,)
tickbox_1.grid(row=0,column=0)
b = Button(custom,command=getinfo)
b.grid(row=1,column=0)

custom.mainloop()

root.mainloop()

Answer

The problem has something to do with calling Tk() twice. You can fix that by explicitly creating a second Toplevel window.

from tkinter import *

root = Tk()

def open_custom_gui():
    custom_gui()

b = Button(root, command=open_custom_gui)
b.grid(row=1, column=0)

def custom_gui():
    def getinfo():
        print(var1.get())

    custom = Toplevel()  # CHANGE THIS (don't call Tk() again)
    var1 = IntVar()
    tickbox_1 = Checkbutton(custom, text='TEST', variable=var1)
    tickbox_1.grid(row=0, column=0)
    b = Button(custom, command=getinfo)
    b.grid(row=1, column=0)

    custom.mainloop()

root.mainloop()
Comments