Python tkinter - 成功从顶层继承

标签 python inheritance tkinter toplevel

我正在尝试使用面向对象的方法来创建一个继承自 tkinter 的 Toplevel 的类,通过按下主窗口中的按钮来触发。

当前代码引发 AttributeError(“MakeWindow”对象没有属性“tk”)。谁能指出我正确的方向?

#! python3
import tkinter as tk


class Application:
    def __init__(self, master):
        self.frame = tk.Frame(master)
        self.frame.pack()    
        self.okButton = tk.Button(self.frame, text="OK",
                                  command=self.window_maker).pack()
        self.quitButton = tk.Button(self.frame, text="Close",
                                    command=self.frame.quit).pack()
    def window_maker(self):
        MakeWindow("A message to Toplevel")


class MakeWindow(tk.Toplevel):
    def __init__(self, message):
        super().__init__(self)
        self.message = message
        self.display = tk.Label(self, text=message)
        self.display.pack()


if __name__ == '__main__':
    root = tk.Tk()
    app = Application(root)
    root.mainloop()

完整回溯:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\r\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1550, in __call__
    return self.func(*args)
  File "C:/Users/r/PycharmProjects/tkinter_gui/y.py", line 15, in window_maker
    MakeWindow("A message to Toplevel")
  File "C:/Users/r/PycharmProjects/tkinter_gui/y.py", line 20, in __init__
    super().__init__(self)
  File "C:\Users\r\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2182, in __init__
    BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
  File "C:\Users\r\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2132, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Users\r\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2110, in _setup
    self.tk = master.tk
AttributeError: 'MakeWindow' object has no attribute 'tk'

最佳答案

问题是 super().__init__(self) 它应该是 super().__init__()。此外,在这种情况下没有必要使用 super (参见 What does 'super' do in Python? )。以下代码有效:

import tkinter as tk


class Application:
    def __init__(self, master):
        self.frame = tk.Frame(master)
        self.frame.pack()    
        self.okButton = tk.Button(self.frame, text="OK",
                                  command=self.window_maker).pack()
        self.quitButton = tk.Button(self.frame, text="Close",
                                    command=self.frame.quit).pack()
    def window_maker(self):
        MakeWindow("A message to Toplevel")


class MakeWindow(tk.Toplevel):
    def __init__(self, message):
        tk.Toplevel.__init__(self) #instead of super
        self.message = message
        self.display = tk.Label(self, text=message)
        self.display.pack()


if __name__ == '__main__':
    root = tk.Tk()
    app = Application(root)
    root.mainloop()

关于Python tkinter - 成功从顶层继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38309288/

相关文章:

javascript - javascript中继承类的最佳实践

javascript - 在击键事件期间难以访问类方法

javascript - 如何组织复杂的 jquery 应用程序?

python - 如何同时给图片添加对比度、亮度等多种效果

python - 在不使用滚动条的情况下在 Python Canvas 上移动

python - 使用 python (pandas) 对 CSV 文件进行条件合并

Python:调用有延迟的函数,而不减慢主循环

Python 三元运算符

python - 如何格式化爬虫输出

python - tkinter 数学程序类型错误