0

In reference to In tkinter GUI what are pending events and idle callbacks? question, which asks what are pending events and idle callbacks in tkinter, I was wondering if there is a way to print the event/idle queue to console or a tracelog.

The only potential solution I could come up with is the code below, but I was wondering if there was an easier way.

import tkinter as tk

root = tk.Tk()

# Some widgets
for foo in range(5):
    tk.Button(root, text="Press Me!").pack()

# Bind all widgets to print events
widgets = root.winfo_children()
def event_print(event):
    print(event)

events = [
    "<Button>",
    "<Motion>",
    "<ButtonRelease>",
    "<Double-Button>",
    "<Enter>",
    "<Leave>",
    "<FocusIn>",
    "<FocusOut>",
    "<Return>",
    "<Key>",
    "<Shift-Up>",
    "<Configure>",
]

for widget in widgets:
    for event in events:
        widget.bind(event, event_print)

root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
jezza_99
  • 1,074
  • 1
  • 5
  • 17
  • I've been using tk for a couple of decades and never once needed to know what was in the event queue. Can you give an example of why you think this information would be useful? – Bryan Oakley Jan 17 '22 at 19:47
  • I wanted to use it as a learning tool for how tk processes events/what events actually occur during the running of tk. But as for a practical use I don't see one, was more for curiousities sake – jezza_99 Jan 17 '22 at 20:27

1 Answers1

1

I was wondering if there is a way to print the event/idle queue to console or a tracelog.

No, there is not. At least, not without writing your own code in C that hooks into the underlying tcl interpreter.

The solution you propose in your question wouldn't work 100% either. There are many events that get added to the queue by the tk library itself.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Makes sense, yeah I didn't think my example was a good solution, just the only one I could come up with. Thanks – jezza_99 Jan 17 '22 at 19:26