2

In a Tkinter GUI, during window resizing at runtime, I've noticed that the removal of a widget or a frame, using my_widget.destroy(), resets the window size to the original shape (the one it had at the start of the resizing). I've found an identical question HERE, with no solution. Couldn't find anything else online.

Is there a way of calling a destroy without interrupting the resize action and resetting the size?

Here is a sample code to test this behavior (creates an empty window of width 800, use the mouse to make it smaller than 600):

import tkinter as tk


class TestResize(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("800x400")
        self.frame1 = tk.Frame(self)
        self.frame1.pack(fill=tk.BOTH, expand=1)
        self.bind("<Configure>", self.resizing)

    def resizing(self, *args):
        self.update_idletasks()
        if self.winfo_width() < 600:
            self.frame1.pack_forget()
            self.frame1.destroy()


def main():
    root = TestResize()
    root.mainloop()


if __name__ == '__main__':
    main()
  • I don't notice anything unusual happening when I resize the window. What platform are you seeing this on? – Bryan Oakley Mar 20 '18 at 03:14
  • There are two things that might be contributing to the problem First, there's no need to call `self.update_idletasks()`. Two, when you put a binding on the root window, it will fire for _every_ window, so it's being called both for the frame (and any of its children) along with the root window. – Bryan Oakley Mar 20 '18 at 03:19
  • Hi Brian, thanks for reading my question, I'm on Windows 10. When I resize the window and the width becomes less than 600, my window instantly goes back to 800, the initial width. The problem persists even without the `update_idletask()` and also binding the configure to the frame instead of the root window. On what platform did u try it? – Andrea Pattarini Mar 20 '18 at 03:38
  • I can confirm the issue on Windows 10 as well. Note that can't you always, re-_set_ the size of the window? – Nae Mar 20 '18 at 06:53

1 Answers1

0

Hey I know it's a long time, but I just ran into this in python 3.7 on windows 10 and my workaround is to use: https://stackoverflow.com/a/41930736/7281120

        if win32api.GetKeyState(0x01) >= 0:
            for widget in self.winfo_children():
                widget.destroy()

to prevent destruction when the mousebutton 1 is pressed. It's a hack but it appears to work.

Drew Macrae
  • 101
  • 4