是否有事件或方法可以检测窗口何时打开/重新打开?
我有两个窗口/框架,我使用以下代码在它们之间切换:
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/