samedi 29 mai 2021

when calling a function including a checkbutton after a tkinter window is created the checkbutton value will always be 0

I'm trying to make a function to create a tkinter window holding a checkbutton. this function is called another Tkinter window is made. whenever i do this, the value of the checkbutton is always 0 even if i select it.

from tkinter import *
def go(value):
    print(value)
    value = value.get()
    print("value of checkbox",value)

def test2():
    tk = Tk()


    value = IntVar() 
    checkbutton = Checkbutton(tk, text = "value", onvalue = 1, offvalue = 0,variable =     value)
    checkbutton.pack()

    button = Button(tk,text = "go", command = lambda:[go(value)])
    button.pack()



window = Tk()

text = Label(window,text = "text")
text.pack()

test2()


window.mainloop()

if i do the same thing without the window, the checkbutton works:

from tkinter import *
def go(value):
    print(value)
    value = value.get()
    print("value of checkbox",value)

def test2():
    tk = Tk()


    value = IntVar() 
    checkbutton = Checkbutton(tk, text = "value", onvalue = 1, offvalue = 0,variable =         value)
    checkbutton.pack()

    button = Button(tk,text = "go", command = lambda:[go(value)])
    button.pack()



#window = Tk()

#text = Label(window,text = "text")
#text.pack()

test2()


#window.mainloop()

maybe the check button in Tkinter is a bit fussy with windows




Aucun commentaire:

Enregistrer un commentaire