属性错误:单击按钮时'str'对象没有属性'tk'



下面是我的代码:


window = tk.Tk()

version = tk.Label(text="hi",
fg="orange",
bg="black",
width=20,
height=10,
)
version.pack()
enter = tk.Button(text="START",fg="blue",width=20,height=7)
enter.pack()
def click(event):
version.forget()
enter.forget()
name = tk.Label("name")
entry = tk.Entry()
name.pack()
entry.pack()

enter.bind("<Button-1>", click)

window.mainloop()

正如你所看到的,我想让一个标签和一个条目在开始按钮被点击后出现。

当我点击按钮时,它返回这个错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
File "/Users/name/Desktop/project/gui/main.py", line 19, in click
name = tk.Label("name")
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 3148, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 2566, in __init__
BaseWidget._setup(self, master, cnf)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 2535, in _setup
self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'

我不知道这里发生了什么。

根据错误语句,问题的根源是这一行:

File "/Users/name/Desktop/project/gui/main.py", line 19, in click
name = tk.Label("name")

Label的第一个预期参数是"母窗口"或父窗口。

最新更新