Python 更新按钮回调中的标签时遇到困难
我对Tkinter和Python3.3都是新手,并尝试开发一个简单的GUI。我有一个标签“statusLabel”。当我点击按钮时,我想在按钮的回调中更新标签中的值,但是我得到了错误Python 更新按钮回调中的标签时遇到困难,python,callback,tkinter,label,config,Python,Callback,Tkinter,Label,Config,我对Tkinter和Python3.3都是新手,并尝试开发一个简单的GUI。我有一个标签“statusLabel”。当我点击按钮时,我想在按钮的回调中更新标签中的值,但是我得到了错误 line 12, in reportCallback statusLabel.config(text="Thank you. Generating report...") AttributeError: 'NoneType' object has no attribute 'config' 下面是我的代码
line 12, in reportCallback
statusLabel.config(text="Thank you. Generating report...")
AttributeError: 'NoneType' object has no attribute 'config'
下面是我的代码
from tkinter import *
root = Tk()
Label(root,text="Project folders. Include full paths. One project per line").pack()
Text(root,height=4).pack()
Label(root,text="Standard project subfolders. Include path from project.").pack()
Text(root,height=4).pack()
statusLabel = Label(root,text="Oh, hello.").pack()
def reportCallback():
statusLabel.config(text="Thank you. Generating report...")
b = Button(root, text="Generate Report", command=reportCallback).pack()
root.mainloop()
这条线就是问题所在:
statusLabel = Label(root,text="Oh, hello.").pack()
.pack()
返回None
。您可能希望statusLabel
保存对刚刚创建的Label
对象的引用
请尝试以下方法:
statusLabel = Label(root,text="Oh, hello.")
statusLabel.pack()
例如,请参阅第一个清单中的普通程序:如果您想在已定义的函数中使用statusLabel,您是否可以将其设置为全局状态(此处未定义它->非类型)