0

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?

Nick
  • 9
  • 3
  • 1
    what do you mean by access? it returns the widget object, you can call any of its methods on it (on `e.widget`), the thing that is printed out is simply the string representation of the widget class or sth. anyhow you can also simply do this: `self.root.bind_all("", self.focus)` (because the argument is passed to the function anyways), or if you inherit from `Tk` also this would be possible `self.bind_all("", self.focus)`, also the binding can't be an empty string – Matiiss Aug 25 '21 at 19:43
  • In this case `event.widget` is the second `Entry` widget in that container. Like @ Matiiss said, you can call all of it's methods. Try it: `print(event.widget.get())`. If you want to bind to focusing a widget use: `""`. Also `.bind_all` should be discouraged if you can just call `.bind` – TheLizzard Aug 25 '21 at 20:00

1 Answers1

0

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

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685