事件绑定以检测 grid_remove (Tkinter)

Event binding to detect a grid_remove (Tkinter)

我正在 Python (2.7) 中设置 Tkinter GUI。有一个地方我计划创建一组类似的小部件,使用 grid()grid_remove() 到 select 其中一个将在主要的特定位置可见 window.我有一个工作原型,但唯一的行为障碍是当我 grid_remove() 一个隐藏它的小部件时,小部件保持焦点 - 甚至响应键盘事件。

是否有一些事件绑定可用于自动检测 grid_remove() 并强制小部件将焦点传递到别处?或者我是否必须将焦点更改放在将执行 grid_remove() 本身的按钮代码中?

您想绑定到 <Unmap> 事件。来自官方 tcl/tk 文档 (bind man page):

The Map and Unmap events are generated whenever the mapping state of a window changes.

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.