【发布时间】:2019-03-11 18:18:08
【问题描述】:
我基本上有两个python脚本Test_A和Test_B。这两个都是基于 tkinter 的 GUI。我正在尝试使用另一个 GUI 打开一个 GUI。我在每个 GUI 中都有一个按钮,可以打开另一个,并且与该按钮对应的功能会破坏当前的 GUI。可悲的是,发生的事情是我无法销毁使用另一个类创建的 TK() 对象。有什么办法可以规避这个问题吗?例如,当我运行 Test_A.py 并单击“打开 GUI 2 按钮”时,第二个 GUI 将打开,现在当我单击“打开 GUI 1 个按钮“第一个没有打开,它说 NameError: name “window2” is not defined。
我的两个脚本如下:
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()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
【问题讨论】:
-
抛出的错误是什么?
-
它说“窗口 2”未定义!。我明白这是因为窗口 2 是在 Test_A_class 中定义的。
-
“窗口 2”还是“窗口 2”?当问技术问题时,准确很重要。您应该edit您的问题并添加实际错误和堆栈跟踪。
-
不知道。现在我已经编辑了问题。
标签: python-3.x class inheritance tkinter