Tkinter: calling destroy() during resize resets the window to original shape

大城市里の小女人 提交于 2021-02-10 08:45:14

问题


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()

回答1:


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.



来源:https://stackoverflow.com/questions/49375176/tkinter-calling-destroy-during-resize-resets-the-window-to-original-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!