Toroid
Toroid

Reputation: 137

Tkinter window event <Visibility>

I tried to get an event if the windows visibility is changed. I found out that there is an event called "Visibility". The Operating System is Windows 64bit. So I implemented in the following way:

root.bind('<Visibility>', visibilityChanged)

But I always got the state "VisibilityUnobscured" no matter if there is a window over it or not. What is the normal behaviour of this event? How can I implement a feature like that?

Example Prog:

import tkinter as tk

class GUI:
    def __init__(self, master):
        self.master = master
        master.title("Test GUI")
        self.master.bind('<Visibility>', self.visibilityChanged)
        self.label = tk.Label(master, text="GUI")
        self.label.pack()

        self.close_button = tk.Button(master, text="Close", command=master.quit)
        self.close_button.pack()

    def visibilityChanged(self, event):
        if (str(event.type) == "Visibility"):
            print(event.state)

root = tk.Tk()
my_gui = GUI(root)
root.mainloop()

Upvotes: 6

Views: 3245

Answers (1)

CommonSense
CommonSense

Reputation: 4482

What is the normal behaviour of this event?

It's well described in the docs:
The X server generates VisibilityNotify event whenever the visibility changes state and for any window.

How can I implement a feature like that?

It depends on how far you are going to go in your wishes, since this isn't a trivial task. Thus, don't treat that answer as a complete solution, but as a problem overview and a set of suggestions.

The event problem

Windows OS uses a message-passing model - the system communicates with your application window via messages, where each message is a numeric code that designates a particular event. Application window has an associated window procedure — a function that processes (responds or ignores) all messages sent.

The most generic solution is to set a hook to catch certain events/messages and it's possible either via SetWindowsHookEx or pyHook.

The main problem is to get event, because the Windows WM has no such message as VisibilityNotify. As I said in comment section - one option, on which we can rely, is the z-order (there's possibility to check Visibility of the window, whenever this window changes it's position in z-order).
Therefore our target message is either WM_WINDOWPOSCHANGING or WM_WINDOWPOSCHANGED.

A naive implementation:

import ctypes
import ctypes.wintypes as wintypes
import tkinter as tk


class CWPRETSTRUCT(ctypes.Structure):
    ''' a class to represent CWPRETSTRUCT structure
    https://msdn.microsoft.com/en-us/library/windows/desktop/ms644963(v=vs.85).aspx '''

    _fields_ = [('lResult', wintypes.LPARAM),
                ('lParam', wintypes.LPARAM),
                ('wParam', wintypes.WPARAM),
                ('message', wintypes.UINT),
                ('hwnd', wintypes.HWND)]


class WINDOWPOS(ctypes.Structure):
    ''' a class to represent WINDOWPOS structure
    https://msdn.microsoft.com/en-gb/library/windows/desktop/ms632612(v=vs.85).aspx '''

    _fields_ = [('hwnd', wintypes.HWND),
                ('hwndInsertAfter', wintypes.HWND),
                ('x', wintypes.INT),
                ('y', wintypes.INT),
                ('cx', wintypes.INT),
                ('cy', wintypes.INT),
                ('flags', wintypes.UINT)]


class App(tk.Tk):
    ''' generic tk app with win api interaction '''

    wm_windowposschanged = 71
    wh_callwndprocret = 12
    swp_noownerzorder = 512
    set_hook = ctypes.windll.user32.SetWindowsHookExW
    call_next_hook = ctypes.windll.user32.CallNextHookEx
    un_hook = ctypes.windll.user32.UnhookWindowsHookEx
    get_thread = ctypes.windll.kernel32.GetCurrentThreadId
    get_error = ctypes.windll.kernel32.GetLastError
    get_parent = ctypes.windll.user32.GetParent
    wnd_ret_proc = ctypes.WINFUNCTYPE(ctypes.c_long, wintypes.INT, wintypes.WPARAM, wintypes.LPARAM)

    def __init__(self):
        ''' generic __init__ '''

        super().__init__()
        self.minsize(350, 200)
        self.hook = self.setup_hook()
        self.protocol('WM_DELETE_WINDOW', self.on_closing)

    def setup_hook(self):
        ''' setting up the hook '''

        thread = self.get_thread()
        hook = self.set_hook(self.wh_callwndprocret, self.call_wnd_ret_proc, wintypes.HINSTANCE(0), thread)

        if not hook:
            raise ctypes.WinError(self.get_error())

        return hook

    def on_closing(self):
        ''' releasing the hook '''
        if self.hook:
            self.un_hook(self.hook)
        self.destroy()

    @staticmethod
    @wnd_ret_proc
    def call_wnd_ret_proc(nCode, wParam, lParam):
        ''' an implementation of the CallWndRetProc callback
        https://msdn.microsoft.com/en-us/library/windows/desktop/ms644976(v=vs.85).aspx'''

        #   get a message
        msg = ctypes.cast(lParam, ctypes.POINTER(CWPRETSTRUCT)).contents
        if msg.message == App.wm_windowposschanged and msg.hwnd == App.get_parent(app.winfo_id()):
            #   if message, which belongs to owner hwnd, is signaling that windows position is changed - check z-order
            wnd_pos = ctypes.cast(msg.lParam, ctypes.POINTER(WINDOWPOS)).contents
            print('z-order changed: %r' % ((wnd_pos.flags & App.swp_noownerzorder) != App.swp_noownerzorder))

        return App.call_next_hook(None, nCode, wParam, lParam)


app = App()
app.mainloop()

As you can see, this implementation has a similar behavior as a "broken" Visibility event.

This problem stems from the fact, that you can catch only thread-specified messages, hence application doesn't know about changes in the stack. It's just my assumptions, but I think that the cause of the broken Visibility is same.

Of course, we can setup a global hook for all messages, regardless a thread, but this approach requires a DLL injection, which is an another story for sure.

The visibility problem

It's not a problem to determine obscuration of the window, since we can rely on Graphical Device Interface.

The logic is simple:

  • Represent window (and each visible window, which is higher in the z-order) as a rectangle.
  • Subtract from main rectangle each rectangle and store result.

If final geometrical subtraction is:

  • ... an empty rectangle — return 'VisibilityFullyObscured'
  • ... a set of rectangles — return 'VisibilityPartiallyObscured'
  • ... a single rectangle:
    • if geometrical difference between result and original rectangle is:
      1. ... an empty rectangle — return 'VisibilityUnobscured'
      2. ... a single rectangle — return 'VisibilityPartiallyObscured'

A naive implementation (with self-scheduled loop):

import ctypes
import ctypes.wintypes as wintypes
import tkinter as tk


class App(tk.Tk):
    ''' generic tk app with win api interaction '''
    enum_windows = ctypes.windll.user32.EnumWindows
    is_window_visible = ctypes.windll.user32.IsWindowVisible
    get_window_rect = ctypes.windll.user32.GetWindowRect
    create_rect_rgn = ctypes.windll.gdi32.CreateRectRgn
    combine_rgn = ctypes.windll.gdi32.CombineRgn
    del_rgn = ctypes.windll.gdi32.DeleteObject
    get_parent = ctypes.windll.user32.GetParent
    enum_windows_proc = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)

    def __init__(self):
        ''' generic __init__ '''
        super().__init__()
        self.minsize(350, 200)

        self.status_label = tk.Label(self)
        self.status_label.pack()

        self.after(100, self.continuous_check)
        self.state = ''

    def continuous_check(self):
        ''' continuous (self-scheduled) check '''
        state = self.determine_obscuration()

        if self.state != state:
            #   mimic the event - fire only when state changes
            print(state)
            self.status_label.config(text=state)
            self.state = state
        self.after(100, self.continuous_check)

    def enumerate_higher_windows(self, self_hwnd):
        ''' enumerate window, which has a higher position in z-order '''

        @self.enum_windows_proc
        def enum_func(hwnd, lParam):
            ''' clojure-callback for enumeration '''
            rect = wintypes.RECT()
            if hwnd == lParam:
                #   stop enumeration if hwnd is equal to lParam (self_hwnd)
                return False
            else:
                #   continue enumeration
                if self.is_window_visible(hwnd):
                    self.get_window_rect(hwnd, ctypes.byref(rect))
                    rgn = self.create_rect_rgn(rect.left, rect.top, rect.right, rect.bottom)
                    #   append region
                    rgns.append(rgn)
            return True

        rgns = []
        self.enum_windows(enum_func, self_hwnd)

        return rgns

    def determine_obscuration(self):
        ''' determine obscuration via CombineRgn '''
        hwnd = self.get_parent(self.winfo_id())
        results = {1: 'VisibilityFullyObscured', 2: 'VisibilityUnobscured', 3: 'VisibilityPartiallyObscured'}
        rgns = self.enumerate_higher_windows(hwnd)
        result = 2

        if len(rgns):
            rect = wintypes.RECT()
            self.get_window_rect(hwnd, ctypes.byref(rect))

            #   region of tk-window
            reference_rgn = self.create_rect_rgn(rect.left, rect.top, rect.right, rect.bottom)
            #   temp region for storing diff and xor rgn-results
            rgn = self.create_rect_rgn(0, 0, 0, 0)

            #   iterate over stored results
            for _ in range(len(rgns)):
                _rgn = rgn if _ != 0 else reference_rgn
                result = self.combine_rgn(rgn, _rgn, rgns[_], 4)
                self.del_rgn(rgns[_])

            if result != 2:
                #   if result isn't a single rectangle
                #   (NULLREGION - 'VisibilityFullyObscured' or COMPLEXREGION - 'VisibilityPartiallyObscured')
                pass
            elif self.combine_rgn(rgn, reference_rgn, rgn, 3) == 1:
                #   if result of XOR is NULLREGION - 'VisibilityUnobscured'
                result = 2
            else:
                #   'VisibilityPartiallyObscured'
                result = 3

            #   clear up regions to prevent memory leaking
            self.del_rgn(rgn)
            self.del_rgn(reference_rgn)

        return results[result]

app = App()
app.mainloop()

Unfortunately, this approach is far from a working solution, but it's tweakable in a perspective.

Upvotes: 5

Related Questions