0

在我的真实软件中,我有一个主菜单(主窗口)和其他菜单(顶层窗口),用户可以使用放置在主菜单中的一些小部件打开它们。似乎可行,主窗口可以打开其他顶层窗口,但我看到了一个非常大的问题。当我为同一个菜单打开多个窗口时,除了最后一个之外,所有窗口都丢失了放置在它们的小部件中的信息(在我的例子中,是 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 对象)时,没关系,没有问题,但是如果您打开另一个窗口,第一个窗口会丢失放置在他的小部件中的信息。为什么?

在此处输入图像描述

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

4

1 回答 1

3

由于您只创建了SecondWindow()inside的一个实例MainWindow,因此每当执行GUI()of时,都会重新分配 的另一个实例,因此先前创建的实例 of没有对其的变量引用,并且它被垃圾收集。SecondWindowself.MenuSVStringVar()StringVar()

您可以在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() 

或保留self.MenuSVinside的参考SecondWindow

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()
于 2021-02-15T03:00:12.357 回答