Skip to content Skip to sidebar Skip to footer

Check If Window Is In Background Tkinter

So, I'm trying to make an app on tkinter. I've just started learning how this module works. In my app, I have a root window and a child (top leveled) window, and I set the child to

Solution 1:

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 tk


classApp(tk.Tk):
    def__init__(self):
        super().__init__()

        self.is_toplevel()

    defis_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)
                                                ) isnotNoneprint('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 - LinuxNote: Confirmed, works on Windows. May not work on MACOS.

Post a Comment for "Check If Window Is In Background Tkinter"