cjm
cjm

Reputation: 844

Python won't recognise attribute

I am coding my first GUI application to find products for a company.

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()

When I run this, it comes up with the error message:

AttributeError: Button instance has no attribute 'text'

But in the creation of 'fire' it says

text="fire"

Isn't this an attribute?

Upvotes: 3

Views: 114

Answers (2)

dlx89
dlx89

Reputation: 1

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

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

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1124758

The Tkinter module is a little old-skool; the text value can be accessed via an item lookup:

relStatus.set(fire['text'])

See the Setting Options section of the Tkinter documentation.

Upvotes: 5

Related Questions