Python Forum
[Tkinter] What can the event.widget reference be used for?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] What can the event.widget reference be used for?
#1
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 receiving the focus. I'm sure it can be done otherwise it wouldn't be useful to report this value.

How does one use ".!entry2" to access the associated widget?
Reply
#2
When the event is triggered, you can use:
print(vars(event))
which will show a dictionary, something like:
Output:
{'serial': 178, 'num': 1, 'height': '??', 'keycode': '??', 'state': 16, 'time': 1126658485, 'width': '??', 'x': 93, 'y': 20, 'char': '??', 'send_event': False, 'keysym': '??', 'keysym_num': '??', 'type': <EventType.ButtonPress: '4'>, 'widget': <tkinter.Button object .!button>, 'x_root': 986, 'y_root': 548, 'delta': 0}
Reply
#3
(Aug-26-2021, 01:41 AM)Larz60+ Wrote: When the event is triggered, you can use:
print(vars(event))
which will show a dictionary, something like:
Output:
{'serial': 178, 'num': 1, 'height': '??', 'keycode': '??', 'state': 16, 'time': 1126658485, 'width': '??', 'x': 93, 'y': 20, 'char': '??', 'send_event': False, 'keysym': '??', 'keysym_num': '??', 'type': <EventType.ButtonPress: '4'>, 'widget': <tkinter.Button object .!button>, 'x_root': 986, 'y_root': 548, 'delta': 0}

I hadn't run into vars() so it's good to know. It provides a lot more info. Thanks! Unfortunately, I don't see any kind of object reference that would allow me to do something like widget_in_focus = get_widget_by_id(event.widget_id).

I do have a tracking list hack, but I'd rather not lean on it. I'm sure I'm just missing something, but I can't find it I'm curious about the notation .!entry2. Why the dot bang?
Reply
#4
Refer to tkinter widget by its instance
Reply
#5
(Aug-26-2021, 05:22 AM)Yoriz Wrote: Refer to tkinter widget by its instance

Bingo and Yahtzee! This solution is perfect.

The function nametowidget(event.widget) is streamlined for interrogating the details of the widget receiving focus.

Many thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 852 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  [Tkinter] moniter which widget triggerd event catlessness 5 2,394 Mar-07-2020, 01:38 AM
Last Post: Larz60+
  pygtk2, how to disconnect all callback of widget or window event ? harun2525 1 3,298 Feb-19-2017, 11:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020