Tkinter:在调整大小期间调用 destroy() 会将窗口重置为原始形状 [英] Tkinter: calling destroy() during resize resets the window to original shape

查看:25
本文介绍了Tkinter:在调整大小期间调用 destroy() 会将窗口重置为原始形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Tkinter GUI 中,在运行时调整窗口大小期间,我注意到使用 my_widget.destroy() 删除小部件或框架会将窗口大小重置为原始形状(它在调整大小开始时的那个).我发现了一个相同的问题 HERE,没有解决方案.在网上找不到其他任何东西.

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?

以下是测试此行为的示例代码(创建一个宽度为 800 的空窗口,使用鼠标使其小于 600):

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

推荐答案

嘿,我知道这已经很长时间了,但我刚刚在 Windows 10 上的 python 3.7 中遇到了这个问题,我的解决方法是使用:https://stackoverflow.com/a/41930736/7281120

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

防止在按下鼠标按钮 1 时破坏.这是一个黑客,但它似乎有效.

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

这篇关于Tkinter:在调整大小期间调用 destroy() 会将窗口重置为原始形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆