Bla*_*den 5 python user-interface class tkinter python-3.x
尝试使用类创建 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
Run Code Online (Sandbox Code Playgroud)
我的代码是:
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()
Run Code Online (Sandbox Code Playgroud)
我想评论你,但有很多话要说:
首先,去掉 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()您在 中 不必要地使用super(Number1, self).__init__()。请阅读 Bryan 的答案:Best way to Structure a tkinter application,并将该行替换为这一行:(tk.Frame.__init__(self, master)为了将来,考虑到Python 的 Super 很漂亮,但你不能使用它)
关于这一行::self.TopTitle = Label("Number1", font = ('Calibri ', 16))传递给tk.Label()(以及您将创建的任何其他小部件)的第一个选项是父小部件:在您的情况下,self.master
我发现这两行与self.TopTitle无用相关,我不明白你想用它们实现什么(此外,你不应该那样命名该标签;如果你想加入 Python 教派,请尊重PEP 8 )
| 归档时间: |
|
| 查看次数: |
9492 次 |
| 最近记录: |