TypeError:super()参数1必须是type,而不是classobj

2024-06-16 11:30:53 发布

您现在位置:Python中文网/ 问答频道 /正文

from Tkinter import *

class Application(Frame):
    def __init__(self, master):
        super(Application, self).__init__(master)
        self.grid()
        self.bttnClicks = 0
        self.createWidgets()

    def createWidgets(self):
        self.bttn = Button(self)
        self.bttn["text"] = "number of clicks"
        self.bttn["command"] = self.upadteClicks
        self.bttn.grid()


    def upadteClicks(self):
        self.bttnClicks += 1
        self.bttn["text"] = "number of clicks " + str(self.bttnClicks)

root = Tk()
root.title("button that do something")
root.geometry("400x200")
app = Application(root)
root.mainloop()`

这就是错误:

super(Application, self).__init__(master)
TypeError: super() argument 1 must be type, not classobj

我做错什么了?代码在Python3.XX中运行良好,但在Python2.XX中却没有


Tags: oftextselfmasternumberapplicationinitdef
2条回答

TKinter.Frame是Python 2上的一个旧样式类。像super这样的功能不能使用它。直接参考Frame.__init__

Frame.__init__(self, master)

Frame不是新样式的类,但是super需要新样式的类才能工作。在python-3.x中,一切都是一个新样式的类,super将正常工作。

您需要在Python2中硬编码超类和方法:

Frame.__init__(self, master)

就像他们在official documentation里那样。

相关问题 更多 >