Python не распознает атрибут


Я кодирую свое первое приложение 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'
Но в творении "огня" сказано: {[4]]}
text="fire"

Разве это не атрибут?

2 3

2 ответа:

Модуль Tkinter является немного старомодным; значение text можно получить с помощью поиска элемента:

relStatus.set(fire['text'])

Смотрите раздел параметры настройки документации Tkinter.

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

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