2013-03-20 57 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