【问题标题】:Opening and closing GUI's using class concept使用类概念打开和关闭 GUI
【发布时间】: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


【解决方案1】:

经过一番思考,我找到了解决方案。我现在所做的是,我已经在各个类的“____init____”方法中声明了 Tk() 变量。这使我能够避免在执行时向类提供参数。接下来,我在销毁当前 GUI 后使用导入函数来调用另一个类。每当调用一个新类时,它的__init____ 函数都会自动声明 Tk()。以前,Windows 是在类之外声明的,因此它们在类中没有作用域。希望这可以帮助一些人。我在这里发布了更新的代码。如果我们可以进一步改进,请告诉我。

from tkinter import *
from Test_B import *


class Test_A_class(Frame):

    def F1(self):
        self.Window1.destroy()
        import Test_B
        Tool = Test_B.Test_B_class()

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

    def __init__(self):
        self.Window1 = Tk()
        super().__init__()
        self.pack()
        self.widgets()


if __name__ == "__main__":
    Tool = Test_A_class()

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

from tkinter import *
from Test_A import *


class Test_B_class(Frame):

    def F2(self):
        self.Window2.destroy()
        import Test_A
        Tool = Test_A.Test_A_class()

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

    def __init__(self):
        self.Window2 = Tk()
        super().__init__()
        self.pack()
        self.widgets()

if __name__ == "__main__":    
    Tool = Test_B_class()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    相关资源
    最近更新 更多