检查窗口是否在后台Tkinter中 [英] Check if window is in background Tkinter
问题描述
因此,我正在尝试在 tkinter
上制作一个应用.我刚刚开始学习此模块的工作原理.
So, I'm trying to make an app on tkinter
. I've just started learning how this module works.
在我的应用程序中,我有一个根窗口和一个子窗口(最高级别),并将该子窗口设置为始终位于顶部.当我最小化根窗口时,子窗口也将最小化,因为我已经定义了该条件.我的问题是当我选择其他窗口时.当我这样做时,子窗口仍然停留在顶部,我想知道是否有一种方法可以知道我的根窗口是否在后台,也就是:我目前不在使用它(例如 root).winfo _...
函数).
In my app, I have a root window and a child (top leveled) window, and I set the child to be always on top. When I minimize my root window, the child window also minimizes, because I have defined that condition. My problem is when I select other window. When I do it, the child window still stays on top and I want to know if there is a way to know if my root window is in background, a.k.a.: I'm not currently working on it (like a root.winfo_...
function).
我可以提供其他示例,因为我觉得我并没有以您理解的方式来解释我的问题.我也可以提供我的代码,但是我认为现在是必须的.
I can provide other examples as I feel that I am not explaining my problem in a way that you understand. I can also provide my code but I think that is now necessary.
推荐答案
问题:检查窗口是否在后台
使用 tk.self.winfo_ contains(...
,您可以确定是否在顶层中显示窗口小部件(此处是 root
窗口)>.在此示例中,给定窗口的中心用作可见点.
Using tk.self.winfo_containing(...
you can determine if a widget, here the root
window, is shown at Top Level.
In this example, the center of the given window is used as visible point.
注意:在移动窗口时,结果可能为 False
.
Note: While you move the window, the result may be False
.
参考:- Tkinter.Widget.winfo_ contains-method
将小部件返回给定位置,或者返回无
Returns the widget at the given position, or None
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.is_toplevel()
def is_toplevel(self):
width, height, x, y = self.winfo_width(), self.winfo_height(), \
self.winfo_rootx(), self.winfo_rooty()
if (width, height, x, y) != (1, 1, 0, 0):
is_toplevel = self.winfo_containing(x + (width // 2),
y + (height // 2)
) is not None
print('is_toplevel: {}'.format(is_toplevel))
self.after(2000, self.is_toplevel)
if __name__ == "__main__":
App().mainloop()
使用Python测试:3.5-'TclVersion':8.6'TkVersion':8.6-Linux
注意:已确认,可在 Windows 上运行.
可能不适用于 MACOS .
Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6 - Linux
Note: Confirmed, works on Windows.
May not work on MACOS.
这篇关于检查窗口是否在后台Tkinter中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!