V.Bon
V.Bon

Reputation: 71

Placing text into an entry box error

I am trying to create a text box that includes text inside of the box but I keep getting an error saying entry has no attribute set. Is there also a way to change the font and size of the entry box.

    HeightEntry = Entry(master, textvariable=Height)
    HeightEntry.pack()
    HeightEntry.set("a default value")
    Height = Height.get()

    Height= StringVar()

Upvotes: 0

Views: 48

Answers (1)

Steven Rumbalski
Steven Rumbalski

Reputation: 45542

Create the StringVar before creating the Entry. Don't call Entry.set to set the value, rather call StringVar.set:

HeightVar = StringVar() # now created before creating Entry
HeightEntry = Entry(master, textvariable=HeightVar)
HeightVar.set("a default value") # now called on the StringVar, not on Entry
HeightValue = HeightVar.get() # returns "a default value" from previous line

You can also do this without StringVar by using delete and insert:

HeightEntry.delete(0, 'end') # needed only if Entry contains text
HeightEntry.insert(0, 'a default value')

For changing the size and the font see The Tkinter Entry Widget by Fredrik Lundh or The Entry Widget by John Shipman.

Upvotes: 1

Related Questions