gpt4 book ai didi

python - 类型错误 : metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

转载 作者:太空宇宙 更新时间:2023-11-03 11:20:30 27 4
gpt4 key购买 nike

尝试使用类创建 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()

最佳答案

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

  • 首先,去掉 from 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(Number1, self).__init__() 中不必要地使用了 super()。请在这里阅读布莱恩的回答: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 相关的两行没用,我不明白你想用它们实现什么(此外,你不应该那样命名那个标签;请加入Python教派请尊重PEP 8)

关于python - 类型错误 : metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43819245/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com