删除或禁用 Windows 下可调整大小的 Tkinter 窗口最大化按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4333341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:43:12  来源:igfitidea点击:

Removing or disabling a resizable Tkinter window maximise button under Windows

pythonwindowseventstkintermaximize

提问by Malcolm

Looking for advice on how to remove or disable a resizablewindow's maximize button under Windows. I'm using Python 2.7 for Windows.

寻求有关如何在 Windows 下删除或禁用可调整大小窗口的最大化按钮的建议。我在 Windows 上使用 Python 2.7。

Background: I have a resizable window with min and max sizes set via the window's minsize() and maxsize() methods. When a user clicks the maximize button, the window moves to the upper left of the display. This is very confusing to my users so I would like to prevent that behavior.

背景:我有一个可调整大小的窗口,通过窗口的 minsize() 和 maxsize() 方法设置最小和最大大小。当用户单击最大化按钮时,窗口将移动到显示屏的左上角。这让我的用户感到非常困惑,所以我想防止这种行为。

My research shows several ways to disable a maximize button - but none of these techniques seem to apply to resizable windows?

我的研究显示了几种禁用最大化按钮的方法 - 但这些技术似乎都不适用于可调整大小的窗口?

  1. Prevent a window from resizing via the resizable( False, False ) method.

  2. Remove all the window's controls (and border) via the overrideredirect( True ) method.

  3. Use the mysterious transient(1) method (this raises an exception under Windows).

  4. Bind the window's event and try to block the maximize (update: Tkinter has already maximized the window by the time our handler detects the maximize event. Returning "break" or using geometry(size|position) to size and re-position a window are both ignored)

  5. I've posted a question on the Python Win32 API mailing list to see if we can use a Windows API call to disable a window's maximize button via the hWnd handle that I believe(?) Tkinter exposes.

  1. 通过 resizable( False, False ) 方法防止窗口调整大小。

  2. 通过 overrideredirect( True ) 方法删除所有窗口的控件(和边框)。

  3. 使用神秘的transient(1) 方法(这会在Windows 下引发异常)。

  4. 绑定窗口的事件并尝试阻止最大化(更新:当我们的处理程序检测到最大化事件时,Tkinter 已经最大化了窗口。返回“break”或使用 geometry(size|position) 来调整和重新定位窗口是都被忽略)

  5. 我在 Python Win32 API 邮件列表上发布了一个问题,看看我们是否可以使用 Windows API 调用通过我相信(?)Tkinter 公开的 hWnd 句柄来禁用窗口的最大化按钮。

Is there a way I can trap the maximize event (BEFORE Tkinter performs it)?

有没有办法可以捕获最大化事件(在 Tkinter 执行之前)?

Malcolm

马尔科姆

回答by rayscan

One possibility is to set the window as a tool window but this also comes with side effects; the window will no longer appear in the taskbar and will have a thin title bar. It will still be able to be resized at the edges.

一种可能性是将窗口设置为工具窗口,但这也会带来副作用;该窗口将不再出现在任务栏中,并将有一个细小的标题栏。它仍然可以在边缘调整大小。

root.attributes("-toolwindow", 1)

More about the root attributes can be found in this guidebook.

可以在本指南中找到有关根属性的更多信息。