更改 Checkbutton 状态时,tkinter 变量不会更改 [英] tkinter variable does not change when changing Checkbutton state

查看:36
本文介绍了更改 Checkbutton 状态时,tkinter 变量不会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口,它打开另一个窗口询问设置.但是我用来获取 Checkbutton 状态的 BooleanVar 没有改变.但是,当我从没有其他窗口的代码中调用 settingswindow 海峡时,它会这样做.

I have a Window wich opens another window askig for the settings. But the BooleanVar I use to get the Checkbutton's state doesn't change. It does however when I call the settingswindow strait from the code without the other window.

这是获得

from tkinter import *


class MainWindow():

    def __init__(self, master):

        self.root = master
        SettingsWindow()
        self.root.mainloop()

class SettingsWindow():

    def __init__(self):
        rootSettings = Tk()

        self.rebuild = BooleanVar()
        chkRebuild = Checkbutton(rootSettings, text="rebuild", variable=self.rebuild, command=self.testFunc)
        chkRebuild.pack()

        rootSettings.mainloop()

    def testFunc(self):
        print(self.rebuild.get())

root = Tk()
mainWindow = MainWindow(root)

点击checkbutton时输出始终为0,即使BooleanVar的输出为True或False.

The output is always 0 when clicking on the checkbutton, evenso the output of BooleanVar is True or False.

从课堂内部或外部调用 SettingsWindow() 有什么区别?至少我认为这是它不起作用的原因.

What is the difference in calling SettingsWindow() from insite a class or outside? At least I think that is the reason it is not working.

我使用 Pyhton3 以防万一.

I am using Pyhton3 in case there is a difference.

推荐答案

Tk 不能有两个实例.对于您的第二个窗口,您需要创建一个 Toplevel.您也不应该在整个程序中多次调用 mainloop.

You can't have two instances of Tk. For your second window you need to create a Toplevel. You also should never call mainloop more than once in your entire program.

这篇关于更改 Checkbutton 状态时,tkinter 变量不会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆