【发布时间】:2014-07-07 22:03:24
【问题描述】:
在努力解决如何将 tkinter Frame 和 LabelFrame 子类化以便他们坐在正确的父类上时,我发现很多答案表明 super().__init__ 在子类化时优于 BaseClass.__init()__。
所以我试了一下,看看有什么大惊小怪的,它似乎根本不起作用。在 python 2.7 中,它抱怨参数的类型。在 3.4 中,它说 master 有多个定义。将 super 注释如下,它按预期工作。我做错了什么?
# import Tkinter as tki # py 2.7
import tkinter as tki # py 3.4
class App(tki.LabelFrame):
def __init__(self, parent):
tki.LabelFrame.__init__(self, master=parent, text='inner')
# super().__init__(self, master=parent, text='inner') #py 3.4
# super(App, self).__init__(self, master=parent, text='inner') #py 2.7
# super(App, self).__init__(master=parent, text='inner') #py 2.7
self.quit = tki.Button(self, text='quit', command=exit)
self.quit.grid()
if __name__ == '__main__':
root = tki.Tk()
root.title('nesting testing')
outer = tki.LabelFrame(root, text='outer level')
outer.pack()
app = App(outer)
app.pack()
root.mainloop()
在对super().__init__() 的调用中删除self 是我尝试的第一件事,但在py2.7 中我仍然收到相同的错误消息,无论它是否存在。
没有self:
Traceback (most recent call last):
File "C:\Python\TESTS\test_super.py", line 21, in <module>
app = App(outer)
File "C:\Python\TESTS\test_super.py", line 9, in __init__
super(App, self).__init__(master=parent, text='inner') #py 2.7
TypeError: must be type, not classobj
与self:
Traceback (most recent call last):
File "C:\Python\TESTS\test_super.py", line 21, in <module>
app = App(outer)
File "C:\Python\TESTS\test_super.py", line 8, in __init__
super(App, self).__init__(self, master=parent, text='inner') #py 2.7
TypeError: must be type, not classobj
错误消息与我删除 self 保持相同的事实表明这不是问题,但它在 BaseClass 调用中正常工作的事实让我对其他参数可能有什么问题感到困惑。
【问题讨论】:
-
总结来自@Veedrac 的非常有用的链接:您不能在Python 2.7 中将
super与该类一起使用,因为它是一个“旧样式”类(它不继承自object)。在 Python 3 中,所有类都是“新样式”,因此super可以工作(包括新的无参数版本)。
标签: python tkinter class-hierarchy