1

I am currently working with curio and am trying to convert tkinter's callbacks into something compatible with asynchronous code. I have most of the things figured out, the event waiting, protocols, but the only thing left is the window updating.

The tkinter.Tk.update method blocks while the window is resized. This screws my asynchronous code as everything must run without blocking. I can't run this in another thread, as this answer notes.

Here is some test code that will time how long the call to update() takes.

import tkinter
from time import monotonic

window = tkinter.Tk()
try:
    while True:
        started = monotonic()
        window.update() # This is the blocking call
        delta = monotonic() - started
        if delta >= 0.5:
            print(f'{func.__name__} took {delta} seconds to run')

except tkinter.TclError:
    # The window is closed
    pass

You can try resizing the window for a second, and the print function runs. If you leave it, nothing happens. This means that the event loop is always processing events from the resizing.

Note: Moving your mouse across the screen won't trigger it, event though with a little modification to check for events, the <Motion> events happen way more than the <Configure> events.

I can currently bypass this issue by running tkinter.Tk.resizable(False, False) to prevent resizing. I'd very much like to work without this restriction, and I am interested as to why the call blocks. Perhaps there are events being processed internally by the event loop, but I'm not sure.

GeeTransit
  • 1,458
  • 9
  • 22

1 Answers1

1

Why does tkinter.Tk.update() block while the window is being resized?

I think the answer is because update must process all of the events in the event queue before it returns, and resizing a window sends a steady stream of events to the event queue as you drag an edge or corner of the window.

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