TypeError: super() argument 1 must be type, not classobj

TypeError: super() argument 1 must be type, not classobj

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

我做错了什么?该代码在 python 3.XX 中运行良好,但在 python 2.XX 中却没有。

TKinter.Frame 是 Python 2 上的旧式 class。super 等功能无法使用它。直接参考Frame.__init__

Frame.__init__(self, master)

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

您需要在 python 2:

中对 superclass 和方法进行硬编码
Frame.__init__(self, master)

就像他们在 official documentation 中所做的那样。