打开和关闭GUI的使用类概念

时间:2019-03-11 18:18:09

标签: python-3.x class inheritance tkinter

我基本上有两个Python脚本Test_A和Test_B。这两个都是基于tkinter的GUI。我正在尝试使用另一个GUI打开一个GUI。我在每个GUI中都有一个按钮,可以打开另一个按钮,并且与该按钮相对应的功能会破坏当前的GUI。可悲的是,发生的事情是我无法销毁使用另一个类创建的TK()对象。有什么办法可以避免这个问题?例如,当我运行Test_A.py并单击“ 打开GUI 2按钮 ”时,第二个GUI打开,现在单击“ 打开GUI” 1个按钮 ”,第一个按钮不会打开,并显示NameError:未定义名称“ window2”。

我的两个脚本如下:

from tkinter import *
from Test_B import *


class Test_A_class(Frame):

    def F1(self):
        Window1.destroy()
        Window2 = Tk()
        Tool = Test_B_class(Window2)
        Window2.mainloop()

    def widgets(self):
        self.Button = Button(self, command=self.F1, text="Open GUI 2", width=15)
        self.Button.pack()        

    def __init__(self, initial):
        super().__init__()
        self.pack()
        self.widgets()

if __name__ == "__main__":

    Window1 = Tk()
    Tool = Test_A_class(Window1)
    Window1.mainloop()

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

from tkinter import *
from Test_A import *


class Test_B_class(Frame):

    def F2(self):
        Window2.destroy()
        Window1 = Tk()
        Tool = Test_A_class(Window1)
        Window1.mainloop()

    def widgets(self):
        self.Button = Button(self, command=self.F2, text="Open GUI 1", width=15)
        self.Button.pack()        

    def __init__(self, initial):
        super().__init__()
        self.pack()
        self.widgets()

if __name__ == "__main__":    
    Window2 = Tk()
    Tool = Test_B_class(Window2)
    Window2.mainloop()

错误如下:

Window2.destroy()
NameError: name 'Window2' is not defined

0 个答案:

没有答案