【问题标题】:Tkinter widgets lost information when I open a new instance of their window当我打开其窗口的新实例时,Tkinter 小部件丢失了信息
【发布时间】:2021-02-15 02:42:46
【问题描述】:

在我的真实软件中,我有一个主菜单(主窗口)和其他菜单(顶层窗口),用户可以使用放置在主菜单中的一些小部件打开它们。似乎可行,主窗口可以打开其他顶层窗口,但我看到了一个非常大的问题。当我为同一个菜单打开多个窗口时,除了最后一个之外,所有窗口都丢失了放置在它们的小部件中的信息(在我的例子中,是 Entry 和 ComboBox 小部件)。让我们从一个简单的例子开始:

from tkinter import *
from tkinter import ttk

class MainWindow:
    def __init__(self):
    
        # load a "SecondWindow" object:
        self.obj=SecondWindow(self)
        
        # main window's gui:
        self.parent=Tk()
        self.parent.geometry("300x280+360+200")
        self.parent.title("main window")
        self.parent.configure(background="#f0f0f0")
        
        self.OkButton=ttk.Button(self.parent, text="open the second window", width=26, command=lambda:self.obj.GUI())
        self.OkButton.place(x=20, y=20)

        self.parent.mainloop() 
    
class SecondWindow:
    def __init__(self, mw):
        self.mw=mw
    
    def GUI(self):
        self.window=Toplevel(self.mw.parent)
        self.window.geometry("300x180+360+200")
        self.window.title("second window")
        self.window.configure(background="#f0f0f0")
            
        self.MenuSV=StringVar()
        self.MenuSV.set("test test test")
        self.MenuComboBox=ttk.Combobox(self.window, state="readonly", values=("ciao", "hola", "hello", "Salut"), textvariable=self.MenuSV)
        self.MenuComboBox.place(x=20, y=20)
        
        self.window.mainloop()

# start the program:
if __name__ == "__main__":
    my_gui=MainWindow()

这段代码就像我的真实软件一样工作。在打开主窗口之前,会加载一个 SecondWindow 对象(他的主要组件是 GUI 函数)。当您只打开第二个窗口一次(使用之前加载的 SeconWindow 对象)时,没关系,没有问题,但是如果您打开另一个窗口,第一个窗口会丢失放置在他的小部件中的信息。为什么?

我真的不明白这种奇怪的行为。我该如何解决这个问题?

【问题讨论】:

    标签: python tkinter toplevel


    【解决方案1】:

    由于您在MainWindow 中仅创建了一个SecondWindow() 实例,因此每当执行SecondWindow 中的GUI() 时,self.MenuSV 将被重新分配另一个StringVar() 实例,因此之前创建的@ 实例987654327@ 没有变量引用,它被垃圾回收了。

    您可以在单击按钮时创建SecondWindow() 的新实例:

    class MainWindow:
        def __init__(self):
        
            # load a "SecondWindow" object:
            #self.obj=SecondWindow(self)
            
            # main window's gui:
            self.parent=Tk()
            self.parent.geometry("300x280+360+200")
            self.parent.title("main window")
            self.parent.configure(background="#f0f0f0")
            
            self.OkButton=ttk.Button(self.parent, text="open the second window", width=26,
                                     command=lambda:SecondWindow(self).GUI()) # create new instance of SecondWindow here
            self.OkButton.place(x=20, y=20)
    
            self.parent.mainloop() 
    

    或者在SecondWindow 中保留self.MenuSV 的引用:

    class SecondWindow:
        def __init__(self, mw):
            self.mw=mw
        
        def GUI(self):
            self.window=Toplevel(self.mw.parent)
            self.window.geometry("300x180+360+200")
            self.window.title("second window")
            self.window.configure(background="#f0f0f0")
                
            self.MenuSV=StringVar()
            self.MenuSV.set("test test test")
            self.MenuComboBox=ttk.Combobox(self.window, state="readonly", values=("ciao", "hola", "hello", "Salut"), textvariable=self.MenuSV)
            self.MenuComboBox.place(x=20, y=20)
            self.MenuComboBox.MenuSV = self.MenuSV  # keep a reference
            
            self.window.mainloop()
    

    【讨论】:

    • 感谢 acw1668!我选择了您的第二个解决方案并且它有效,但我仍然不明白为什么当我打开另一个窗口时self.MenuSV 必须更改。它不是一个独立的“StringVar()”对象吗?
    • self.MenuSV 是对StringVar 对象的引用,而不是StringVar 对象本身。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2013-04-29
    相关资源
    最近更新 更多