3

我正在编写我的第一个 GUI 应用程序来为公司查找产品。

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"

这不是属性吗?

4

2 回答 2

5

Tkinter 模块有点老派。text可以通过项目查找访问该值:

relStatus.set(fire['text'])

请参阅 Tkinter 文档的设置选项部分

于 2012-08-15T09:39:55.180 回答
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
于 2012-08-15T14:18:41.803 回答