2012-08-15 99 views
3

我正在編寫我的第一個GUI應用程序以查找公司的產品。Python將無法識別屬性

from Tkinter import * 
import tkMessageBox 

def debug(): 
    buttonText = relStatus.get() 
    tkMessageBox.showinfo("You Clicked ", buttonText) 
    return 

app = Tk() 
app.title("Ironcraft Product Finder") 
app.geometry("700x500") 

labelText = StringVar() 
labelText.set("Choose an Appliance Type") 
topLabel = Label(app, textvariable = labelText, height = 5).pack() 

fire = Button(app, text="Fire", width=20, command=debug) 
fire.pack(padx=10) 

relStatus = StringVar() 
relStatus.set(fire.text) 

app.mainloop() 

當我運行它,它出現並顯示錯誤消息:

AttributeError: Button instance has no attribute 'text' 

但在創作「火」的,它說

text="fire" 

這不是一個屬性?

回答

5

Tkinter模塊是一個有點舊的skool;所述text值可經由項目查找來訪問:

relStatus.set(fire['text']) 

參見Tkinter的文檔的Setting Options section

+0

謝謝,永遠不會有我自己的:) – cjm 2012-08-15 09:44:38

0
topLabel = Label(app, textvariable = labelText, height = 5).pack() # so topLabel is None 

topLabel = Label(app, textvariable = labelText, height = 5) 
topLabel.pack() # topLabel is Label 
相關問題