What does the event.widget reference mean, and how can you use it to access the widget

101 Views Asked by At

Widgets bound with self.root.bind_all("", lambda e: self.focus(e)) return a widget reference through e.widget such as ".!entry2" when a widget receives the focus.

I can't find anywhere how this notation can be used to identify or access the particular widget. I'm sure it can be done otherwise it wouldn't be useful to report the value.

How does one use ".!entry2" to access the associated widget?

1

There are 1 best solutions below

0
On

I can't find anywhere how this notation can be used to identify or access the particular widget.

It doesn't identify the widget, it is the widget.

How does one use ".!entry2" to access the associated widget?

You don't need to. What you are seeing is just the string representation of the widget. While you can use it to look up the widget instance, there's no reason because you'll just get back the same thing that you already have.

(Pdb) print(event.widget)
.!entry2
(Pdb) type(event.widget)
<class 'tkinter.Entry'>
(Pdb) root.nametowidget(".!entry2") is event.widget
True

If you want to call a method on the widget, you can do it directly on event.widget - eg: event.widget.insert("end", "Hello, world")