2013-03-20 59 views
1

我对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() 
+0

可以,你应该,如果你想在一个定义的函数使用它让你statusLabel全球(它没有在那里定义 - > NoneType) – chill0r 2013-03-20 16:09:09

回答

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