Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有一种方法可以动态更改使用Tkinter显示的小部件?_Python_Tkinter_Dynamic_Widget - Fatal编程技术网

Python 有没有一种方法可以动态更改使用Tkinter显示的小部件?

Python 有没有一种方法可以动态更改使用Tkinter显示的小部件?,python,tkinter,dynamic,widget,Python,Tkinter,Dynamic,Widget,我正在用Tkinter构建一个GUI,在这个GUI中,我希望通过单击按钮,为用户提供将条目小部件更改为标签小部件(反之亦然)的选项 我尝试过几种不同的方法,但都没能奏效。以下是我试图解决这个问题的方法之一: import tkinter as tk show_label = False class App(tk.Tk): def __init__(self): super().__init__() label = tk.Label(self, te

我正在用Tkinter构建一个GUI,在这个GUI中,我希望通过单击按钮,为用户提供将条目小部件更改为标签小部件(反之亦然)的选项

我尝试过几种不同的方法,但都没能奏效。以下是我试图解决这个问题的方法之一:

import tkinter as tk

show_label = False


class App(tk.Tk):

    def __init__(self):
        super().__init__()
        label = tk.Label(self, text="This is a label")
        entry = tk.Entry(self)
        button = tk.Button(self, text="Label/Entry",
                           command=self.change)
        if show_label:
            label.pack()
        else:
            entry.pack()

        button.pack()

    def change(self):
        global show_label
        show_label = not show_label
        self.update()

if __name__ == '__main__':
    app = App()
    app.mainloop()
除上述外,我还尝试过:

  • 在主循环内更新应用程序实例,即在实例化
    app=app()
  • 将show_label设置为类变量,将change()设置为类方法
在此问题上的任何帮助都将不胜感激


谢谢

看来您犯的错误是认为
\uuu init\uuu
中的代码运行了不止一次。当您创建
App
的实例时,它只运行一次

要修复代码,请将显示条目或标签的逻辑移动到单击按钮时运行的代码中。此外,还需要使用实例变量来保存对小部件的引用,以便可以在其他函数中引用它们

import tkinter as tk

class App(tk.Tk):

    def __init__(self):
        super().__init__()
        self.label = tk.Label(self, text="This is a label")
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Label/Entry",
                                command=self.change)
        self.button.pack()
        self.show_label = False

    def change(self):
        self.show_label = not self.show_label

        if self.show_label:
            self.entry.pack_forget()
            self.label.pack()
        else:
            self.label.pack_forget()
            self.entry.pack()


if __name__ == '__main__':
    app = App()
    app.mainloop()

看来您犯的错误是认为
\uuuu init\uuuu
中的代码运行了不止一次。当您创建
App
的实例时,它只运行一次

要修复代码,请将显示条目或标签的逻辑移动到单击按钮时运行的代码中。此外,还需要使用实例变量来保存对小部件的引用,以便可以在其他函数中引用它们

import tkinter as tk

class App(tk.Tk):

    def __init__(self):
        super().__init__()
        self.label = tk.Label(self, text="This is a label")
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Label/Entry",
                                command=self.change)
        self.button.pack()
        self.show_label = False

    def change(self):
        self.show_label = not self.show_label

        if self.show_label:
            self.entry.pack_forget()
            self.label.pack()
        else:
            self.label.pack_forget()
            self.entry.pack()


if __name__ == '__main__':
    app = App()
    app.mainloop()

\uuuu init\uuu
函数在应用程序上只调用一次。窗口中的小部件集是可更改的,但您必须在其他地方执行此操作。另请参见
\uuuu init\uuuu
函数在应用程序上只调用一次。窗口中的小部件集是可更改的,但您必须在其他地方执行此操作。另见