windows Python/Tkinter 窗口事件和属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4038070/
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:29:39  来源:igfitidea点击:

Python/Tkinter window events and properties

pythonwindowsuser-interfaceformstkinter

提问by Malcolm

I've been searching for information on the following Tkinter window features without success. Platform is Windows, Python 2.7. At the end of this post is code that can be used to explore Tkinter window events.

我一直在搜索有关以下 Tkinter 窗口功能的信息,但没有成功。平台是 Windows,Python 2.7。在这篇文章的末尾是可用于探索 Tkinter 窗口事件的代码。

  1. How can one detect window minimize/maximize events? The event object returned by binding to a window's event does contain any information about these events. I've searched for protocols (like WM_DELETE_WINDOW) that might expose these events without success.

  2. How can one determine window frame border sizes (not Tkinter frames, the frame the OS places around the container where Tkinter places its widgets)? Is there a non-platform specific way to discover these windows properties or do I need to use platform specific solutions like the win32 api under Windows?

  3. How can one determine a window's visibility, eg. whether a window is invisible or not as set by .withdraw()?

  4. Is it possible to cancel a window event, eg. if one wanted to constrain a window to a certain location on a user's desktop? Returning 'break' from a window's event does not cancel window move or resize events.

  1. 如何检测窗口最小化/最大化事件?通过绑定到窗口事件返回的事件对象确实包含有关这些事件的任何信息。我已经搜索了可能会公开这些事件但没有成功的协议(如 WM_DELETE_WINDOW)。

  2. 如何确定窗口框架边框大小(不是 Tkinter 框架,操作系统放置在 Tkinter 放置其小部件的容器周围的框架)?是否有非平台特定的方法来发现这些 Windows 属性,或者我是否需要使用特定于平台的解决方案,例如 Windows 下的 win32 api?

  3. 如何确定窗口的可见性,例如。窗口是否按照 .withdraw() 设置的不可见?

  4. 是否可以取消窗口事件,例如。如果想将窗口限制在用户桌面上的某个位置?从窗口事件返回 'break' 不会取消窗口移动或调整大小事件。

Here's sample code for experimenting with Tkinter window events.

这是用于试验 Tkinter 窗口事件的示例代码。

def onFormEvent( event ):
    for key in dir( event ):
        if not key.startswith( '_' ):
            print '%s=%s' % ( key, getattr( event, key ) )
    print

import Tkinter as tkinter
root = tkinter.Tk()
lblText = tkinter.Label( root, text='Form event tester' )
lblText.pack()
root.bind( '<Configure>', onFormEvent )
root.mainloop()

Update: Here's what I learned about the following events:

更新:这是我对以下事件的了解:

  1. event.type == 22 (one or more of following changed: width, height, x, y)

  2. event.type == 18 (minimized) event.widget.winfo_viewable() = 0 (invisible)

  3. event.type == 19 (restore after minimized)

  4. event.type == 2 (maximize)

  5. event.type == 22 (restore after maximized due to change in width and height)

  1. event.type == 22(以下一项或多项更改:宽度、高度、x、y)

  2. event.type == 18(最小化) event.widget.winfo_viewable() = 0(不可见)

  3. event.type == 19(最小化后恢复)

  4. event.type == 2(最大化)

  5. event.type == 22(因宽高变化最大化后恢复)

采纳答案by Brandon

Determining window visibility is done with a .winfo_viewable() call. Returns 1 if visible, 0 if not.

确定窗口可见性是通过 .winfo_viewable() 调用完成的。如果可见则返回 1,否则返回 0。

If you want to prevent the window from resizing, set up your window the way you want, then use

如果您想阻止窗口调整大小,请按照您想要的方式设置窗口,然后使用

self.root.minsize(self.root.winfo_reqwidth(), self.root.winfo_reqheight())
self.root.maxsize(self.root.winfo_reqwidth(), self.root.winfo_reqheight())

at the end of your __init__call.

__init__通话结束时。

To completely disable the window from being moved, then you probably just want to remove the title bar and frame with self.root.overrideredirect(True).

要完全禁止移动窗口,那么您可能只想删除带有self.root.overrideredirect(True).