python-3.x - 检测窗口何时打开

标签 python-3.x tkinter

是否有事件或方法可以检测窗口何时打开/重新打开?

我有两个窗口/框架,我使用以下代码在它们之间切换:

self.frame1.withdraw()
self.frame2.deiconify()

我使用此协议(protocol)来检测帧何时更接近:

self.frame.protocol("WM_DELETE_WINDOW", self.on_closing)

我一直在研究这些协议(protocol),我找到了这个文档,但似乎只有三个协议(protocol)?并且没有一个窗口打开时的信息? https://www.x.org/releases/X11R7.6/doc/xorg-docs/specs/ICCCM/icccm.html

编辑:模块 1 托管框架 1,对象 2 托管框架 2。所有窗口都是在代码执行时创建的,但它们是隐藏的,只有主窗口可见。我使用withdraw() 和deiconify() 在框架之间切换。我希望 Module1 在 Frame1 可见时执行一些操作。

最佳答案

您可以绑定(bind)到<Map>当窗口可见时要通知的事件。

def notify(event):
    print("window was mapped: %s" % event.widget)

window = tk.Toplevel(...)
window.bind("<Map>", notify)

来自官方 tk 文档:

Windows are created in the unmapped state. Top-level windows become mapped when they transition to the normal state, and are unmapped in the withdrawn and iconic states. Other windows become mapped when they are placed under control of a geometry manager (for example pack or grid).

A window is viewable only if it and all of its ancestors are mapped. Note that geometry managers typically do not map their children until they have been mapped themselves, and unmap all children when they become unmapped; hence in Tk Map and Unmap events indicate whether or not a window is viewable.

关于python-3.x - 检测窗口何时打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45537074/

相关文章:

python - 将自定义属性添加到 Tk 小部件

python - 使用文本文档更改列表中的项目

带有网络摄像头的 Python 2.7 CV2 和 Tkinter

Python将对象属性写入文件

python - 使用 urllib 读取 mashabe API

Python3 不可能将 @property 作为装饰器参数传递

multithreading - Python中的错误? threading.Thread.start() 并不总是返回

python - 如何使用递归方式将二进制转换为十进制?

python - 如何查找 **options 参数是什么?

python - (实例化一组按钮,但只有一个有效