【问题标题】:TypeError: super() argument 1 must be type, not classobj [duplicate]TypeError:super()参数1必须是类型,而不是classobj [重复]
【发布时间】:2017-10-01 18:05:47
【问题描述】:
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 中却不行。

【问题讨论】:

    标签: python python-2.7 tkinter super


    【解决方案1】:

    TKinter.Frame 是 Python 2 上的旧式类。super 等功能不适用于它。直接参考Frame.__init__

    Frame.__init__(self, master)
    

    【讨论】:

      【解决方案2】:

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

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

      Frame.__init__(self, master)
      

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

      【讨论】:

        猜你喜欢
        • 2020-11-09
        • 1970-01-01
        • 2021-08-05
        • 2012-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多