I'm having 'fun' with Python TkInter. Unfortunately, my code returns an error
AttributeError: 'str' object has no attribute 'bind'
I've already assigned StringVar
type to my variable, but it doesn't seem to work.
This is my code:
import tkinter
from tkinter import StringVar
from typing import Type
window = tkinter.Tk()
label = tkinter.Label(
text='Hello TkInter',
foreground='white',
background='black',
)
entry = tkinter.Entry(
foreground='yellow',
background='blue',
width=50,
)
UserEntry: Type[StringVar] = entry.get()
def PrintUserEntry():
print(UserEntry.widget.get())
label.pack()
entry.pack()
UserEntry.bind('<Enter>', PrintUserEntry())
window.mainloop()
Any ideas?
UserEntry
as the result ofentry.get()
therefore it will be a string not a tkinter widget. ChangeUserEntry.bind('<Enter>', PrintUserEntry())
toentry.bind('<Enter>', PrintUserEntry)
– TheLizzard<Enter>
is for when the user's mouse hovers over the widget. I think you wanted to use<Return>
which is the enter key on the keyboard. – TheLizzarddef PrintUserEntry()
todef PrintUserEntry(event)
because when you bind an event is passed in the function. – TheLizzardline 1884, in __call__ return self.func(*args)
andline 22, in PrintUserEntry print (UserEntry.widget.get()) AttributeError: 'str' object has no attribute 'widget'
– KrzysztoffUserEntry
is still a string not aStringVar
. Look at my answer bellow. – TheLizzard