Show your Python app on all Window's desktops

Reverend Jim 2 Tallied Votes 788 Views Share

I wrote a timer/alarm app in Python that I wanted to be accessible from all Windows Desktops. What I had to do until recently was run the app, select the desktops icon on my toolbar, then right click the app and select "Show this window on all desktops". I was certain there was a way I could do it from within the app and after a little research I found it. Here is a function you can call to do this. Note the usually bad practice of using an except clause to trap all possible errors.

import win32con
import win32gui

def AllDesktops(title):
    """Make the window with the given title visible on all desktops"""

    try:
        #Get the handle to the window
        hwnd = win32gui.FindWindow(None, title)

        #Set the WS_EX_TOOLWINDOW style
        ex_style = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
        win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, ex_style | win32con.WS_EX_TOOLWINDOW)

        #Show the window on all desktops

        win32gui.SetWindowPos(hwnd, -1, 0, 0, 0, 0,
                              win32con.SWP_NOMOVE |
                              win32con.SWP_NOSIZE |
                              win32con.SWP_SHOWWINDOW |
                              win32con.SWP_NOZORDER)
        return True
    except:
        return False
AndreRet 526 Senior Poster

Awesome code and simple solution to a 'pulling-out-my-hair' problem!

To use this function in an example -

# Usage example
window_title = "Reverend Jim's Application"

# Call the function to show the window on all desktops
success = AllDesktops(window_title)

if success:
    print(f"Window '{window_title}' is now visible on all desktops.")
else:
    print("Failed to make the window visible on all desktops.")

If I may, I will add some more specific error checking on win32gui to handle potential errors from the win32gui functions, check if the hwnd value is checked and if it is 0 (window was not found), a 'ValueError' is raised with an error message.

I will also add the win32con.HWND_TOPMOST constant to let the window be brought to the top of the z-order (show on top) on all desktops.

import win32gui
import win32con


def show_on_all_desktops(title):
    try:
        # Get the handle to the window
        hwnd = win32gui.FindWindow(None, title)

        if hwnd == 0:
            raise ValueError(f"Window '{title}' not found.")

        # Set the WS_EX_TOOLWINDOW style
        ex_style = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
        win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, ex_style | win32con.WS_EX_TOOLWINDOW)

        # Show the window on all desktops
        win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0,
                              win32con.SWP_NOMOVE |
                              win32con.SWP_NOSIZE |
                              win32con.SWP_SHOWWINDOW |
                              win32con.SWP_NOZORDER)
        return True
    except win32gui.error as e:
        print(f"Error: {e}")
        return False


# Usage example
window_title = "My Application"

# Call the function to show the window on all desktops
success = show_on_all_desktops(window_title)

if success:
    print(f"Window '{window_title}' is now visible on all desktops.")
else:
commented: Worthwhile improvements. Thanks. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.