Check if window is in background Tkinter
Question: Check if window is in background
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.
Note: While you move the window, the result may be False
.
Reference:- Tkinter.Widget.winfo_containing-method
Returns the widget at the given position, or None
import tkinter as tkclass 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()
Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6 - Linux
Note: Confirmed, works on Windows.
May not work on MACOS.