TypeError:元类冲突:派生类的元类必须是其所有基类的元类的(非严格)子类

时间:2017-05-06 10:19:49

标签: python python-3.x class user-interface tkinter

尝试使用类创建GUI,并且我一直遇到此错误的问题。我不确定这是什么意思,因为我只能看到一个班级,我的错误是:

Traceback (most recent call last):
File "C:/Users/Blaine/Desktop/Computing Project.py", line 5, in <module>
class SneakerSeeker(tk,Frame):
TypeError: metaclass conflict: the metaclass of a derived class must be a 
(non-strict) subclass of the metaclasses of all its bases

我的代码是:

from tkinter import * 
import tkinter as tk
import tkinter.messagebox as tm

class Number1(tk,Frame):
    def __init__(self, master):
        super(Number1, self).__init__()
        self.master = master
        self.frame = tk.Frame(self.master)
        self.TopTitle = Label("Number1", font = ('Calibri ', 16))
        self.TopTitle.pack()


def main():
    root = tk.Tk()
    root.title("Number 1")
    app = Number1(root)
    root.mainloop()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:2)

我想评论你,但有很多话要说:

  • 首先,摆脱tkinter import *并改为编写import tkinter as tk(正如Bryan在这里写过很多次)。除此之外,在同一个应用程序中编码from tkinter import *import tkinter as tk的目的是什么?执行此操作时,所有窗口小部件类必须以tk开头(tk.Label(...)tk.Frame(...) ...)

  • class Number1(tk,Frame)中,您应该写tk.Frame(或只是Frame,如果您按原样保留进口数据)

  • 您在super()中使用了super(Number1, self).__init__()。请在此处阅读Bryan的答案:Best way to structure a tkinter application,并将此行替换为:tk.Frame.__init__(self, master)(将来,请考虑Python's Super is nifty, but you can't use it

  • 关于这一行:self.TopTitle = Label("Number1", font = ('Calibri ', 16)):传递给tk.Label()的第一个选项(以及您将创建的任何其他小部件)是父窗口小部件:在您的情况下,self.master

  • 我发现与self.TopTitle无关的2行是无用的,我不明白你想要用它们实现的目标(此外,你不应该用那个方式命名那个标签;请尊重{{3}如果你想加入Python教派)