AttributeError: 'str' object has no attribute 'tk' when click button
AttributeError: 'str' object has no attribute 'tk' when click button
这是我的代码:
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()
如您所见,我想在单击“开始”按钮后显示一个标签和一个条目。
但是,当我点击按钮时,它 returns 这个错误:
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 的第一个预期参数是“master”或父级 window。
这是我的代码:
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()
如您所见,我想在单击“开始”按钮后显示一个标签和一个条目。
但是,当我点击按钮时,它 returns 这个错误:
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 的第一个预期参数是“master”或父级 window。