0

我在 tkinter,python 中为检查按钮运行简单的代码。当我检查检查按钮时,代码根据检查按钮的状态打印“1”或“0”。但是当我调用该函数包含来自常规 tkinter 按钮的检查按钮时,我总是得到“0”!!!

此代码运行良好: print("0" or "1" 根据检查统计)

from tkinter import *
def  runcheck():
    window = Tk()
    var=IntVar(value=0)
    Checkbutton(window,  text = "CHECK", variable = var, command=lambda: print(var.get())).grid(row=0, column=0)
    window.mainloop()

runcheck()

但是这段代码不能很好地工作(只打印“0”)

from tkinter import *
def  runcheck():
    window = Tk()
    var=IntVar(value=0)
    Checkbutton(window,  text = "CHECK", variable = var, command=lambda: print(var.get())).grid(row=0, column=0)
    window.mainloop()

window1 = Tk()
Button(window1,  text='BUTTON', command=lambda: runcheck()).grid(row=0, column=0) 
window1.mainloop()
4

0 回答 0