Tkinter:无论是否选中,复选框值始终为0

问题描述

我面临的问题是,每当我选中/取消选中该复选框时,我只会得到以下输出:“值是0”。以下是我正在使用的简单脚本:

import tkinter as tk
from tkinter import *

root = tk.Tk()
root.title("Main app")
#the main frame is empty

#another tab within the software
def agent_module():
    root1 = tk.Tk()
    root1.title("New Tab")

    var1 = tk.Intvar()

    #function executed when checkBox is clicked
    def function():
        if var1.get() == 1:
            print("value is 1")
        elif var1.get() == 0:
            print("value is 0")

    #widget (this case checkBox only) in the tab
    checkBox = tk.Checkbutton(root1,text="Press me",variable=var1,command=function)
    checkBox.grid()

#menu bar configuration
menubar = Menu(root)
filemenu = Menu(menubar,tearoff=0)
filemenu.add_command(label="Software",command=agent_module)
menubar.add_cascade(label="Main",menu=filemenu)

#necessary stuff for the app
root.config(menu=menubar)
root.mainloop()

我的目标是,当我选中/取消选中复选框时,我想查看输出。但是,即使选中此复选框,我唯一的输出是“值是0”。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)