Question:检查窗口是否在后台
Using tk.self.winfo_containing(...
您可以确定是否有一个小部件,这里是root
窗口,显示在顶层。
在此示例中,给定窗口的中心用作visible point.
Note:当您移动窗口时,结果可能是False
.
参考:
- Tkinter.Widget.winfo_containing-method http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.winfo_containing-method
返回给定位置的小部件,或者 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
Note: 已确认,正在运行Windows.
可能无法正常工作MACOS.