难以更新按钮回调中的标签

时间:2013-03-20 16:05:12

标签: python callback tkinter label config

我是Tkinter和Python 3.3的新手,并尝试开发一个简单的GUI。我有一个标签“statusLabel”。当我点击按钮时,我想更新按钮回调中标签中的值,但是我收到了错误。

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()

1 个答案:

答案 0 :(得分:2)

这一行是问题所在:

statusLabel = Label(root,text="Oh, hello.").pack()

.pack()返回None。据推测,您希望statusLabel保留对刚刚创建的Label对象的引用。

请改为尝试:

statusLabel = Label(root,text="Oh, hello.")
statusLabel.pack()

例如,请参阅第一个列表中的简单程序:http://effbot.org/tkinterbook/label.htm