J-S J-S - 1 month ago 8
Python Question

tkinter get <ButtonRelease-1> when resizing window

I am drawing an image gallery on canvas. When the window is resized I need to redraw the gallery.

I can bind the

<configure>
event to get resize notifications. But resizing the window by dragging the mouse creates too many events. I would like to store the change and only redraw the gallery when the mouse button is released.

Problem:
I cannot find a way to get a press or release event on the windows borders.

Is that possible or are the borders not actually part of the window?

Answer

It is not possible to know when the user releases the mouse, if they clicked it on the border of the window. The border is not part of the window, and Tkinter has very little knowledge of events outside of the widgets it creates.

The simplest solution might be to add a delay to your redrawing. In a <Configure> event, cancel any pending redraws, and schedule a new one to happen in one or two seconds.

class Application(...):
    def __init__(self, ...):
        ...
        self._after_id = None
        self.bind("<Configure>", self.schedule_redraw)
        ...

    def schedule_redraw(self, event):
        if self._after_id:
            self.after_cancel(self._after_id)
        self._after_id = self.after(2000, self.redraw)
Comments