类型错误:super() 参数 1 必须是类型,而不是 classobj

mis*_*sza 3 python tkinter super python-2.7

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()`
Run Code Online (Sandbox Code Playgroud)

这就是错误:

super(Application, self).__init__(master)
TypeError: super() argument 1 must be type, not classobj
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?该代码在 python 3.XX 中运行良好,但在 python 2.XX 中则不然。

MSe*_*ert 5

Frame不是新式类,但super需要新式类才能工作。在 python-3.x 中,一切都是新式类,因此super可以正常工作。

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

Frame.__init__(self, master)
Run Code Online (Sandbox Code Playgroud)

就像他们在官方文档中所做的那样。