【问题标题】:Python tkinter - successfully inherit from toplevelPython tkinter - 成功从顶层继承
【发布时间】:2016-07-11 14:18:59
【问题描述】:

我正在尝试使用面向对象的方法来创建一个从 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__() 可以正常工作。目前,您将新的MakeWindow 实例作为master 提供,这是不可能的。
  • 行得通,谢谢!

标签: python inheritance tkinter toplevel


【解决方案1】:

问题是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()

【讨论】:

  • 你为什么不使用super?你不需要,但它更整洁。
  • 谢谢 - 所以如果我使用 super() 不需要'self',tk.Toplevel.__init__(self) 确实需要'self'。 Pycharm 抱怨后者(“预期类型 'Toplevel' 但得到了 'Make Window'”)但它工作正常。谢谢,谢谢你的链接!
猜你喜欢
  • 2021-04-20
  • 2021-01-25
  • 1970-01-01
  • 2023-03-14
  • 2019-09-17
  • 1970-01-01
  • 2014-09-28
  • 2013-04-06
  • 1970-01-01
相关资源
最近更新 更多