python - 类型错误 : super() argument 1 must be type, 不是 classobj

标签 python python-2.7 tkinter super

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 中则不然。

最佳答案

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

您需要在 python 2 中硬编码父类(super class)和方法:

Frame.__init__(self, master)

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

关于python - 类型错误 : super() argument 1 must be type, 不是 classobj,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43767988/

相关文章:

Python Tkinter 绑定(bind)子部件

python - 如何更改 ttk.Combobox 中下拉菜单(即 tk.Listbox)事件背景的颜色

python - 通过电子邮件重置 Django 密码

python - "Unquote"/在python中解析bash参数字符串

python - 基于数组的子集填充列

python - 想要在 python 中执行分组,其中分组的数据将进入行

python - 玛雅 : ScriptNode pop up menu with error "invalid directive"

python - 即使使用 python with 语句,写入文件也无法完成

python - 如何使用按钮销毁程序中当前打开的每个屏幕

python - Pylons:建立每线程/每请求资源的正确方法?