微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。