Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set application's taskbar icon in Windows 7

Tags:

windows-7

qt

pyqt

People also ask

How do I change the icon picture on my taskbar?

Right-click on this name to show another list of options and choose Properties there. This will open a window to the program's properties panel, on the Shortcut tab. There, click the Change Icon button at the bottom. In the new window, you'll be able to select a new icon for the program on your taskbar.

How do I put icons on my taskbar?

To pin apps to the taskbarSelect Start , scroll to the app you want to pin, then press and hold (or right-click) the app. Select More > Pin to taskbar. If the app is already open on the desktop, press and hold (or right click) the app's taskbar icon, and then select Pin to taskbar.

How do I customize apps on my taskbar?

Change your taskbar settingsPress and hold or right-click any empty space on the taskbar, and then select Taskbar settings. In the Taskbar settings, scroll to see the options for customizing, choosing icons, and much more.


I've found the answer, after some digging.

In Windows 7, the taskbar is not for "Application Windows" per se, it's for "Application User Models". For example, if you have several different instances of your application running, and each instance has its own icon, then they will all be grouped under a single taskbar icon. Windows uses various heuristics to decide whether different instances should be grouped or not, and in this case it decided that everything hosted by Pythonw.exe should be grouped under the icon for Pythonw.exe.

The correct solution is for Pythonw.exe to tell Windows that it is merely hosting other applications. Perhaps a future release of Python will do this. Alternatively, you can add a registry key to tell Windows that Pythonw.exe is just a host rather than an application in its own right. See MSDN documentation for AppUserModelIDs.

Alternatively, you can use a Windows call from Python, to explicitly tell Windows what the correct AppUserModelID is for this process:

import ctypes
myappid = 'mycompany.myproduct.subproduct.version' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)

EDIT: Please see Ronan's answer: the myappid string should be unicode.


@DamonJW's answer will work, but there is a minor catch: myappid should be unicode (argument type is PCWSTR).

import ctypes
myappid = u'mycompany.myproduct.subproduct.version' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)

Otherwise getting the AppUserModelID will get wrong unicode characters (祭潣灭湡⹹祭牰摯捵⹴畳灢潲畤瑣瘮牥楳湯):

import ctypes
from ctypes import wintypes
lpBuffer = wintypes.LPWSTR()
AppUserModelID = ctypes.windll.shell32.GetCurrentProcessExplicitAppUserModelID
AppUserModelID(ctypes.cast(ctypes.byref(lpBuffer), wintypes.LPWSTR))
appid = lpBuffer.value
ctypes.windll.kernel32.LocalFree(lpBuffer)
if appid is not None:
    print(appid)

That said, it is a minor thing, since Windows will still recognize the unicode string as "another process" and switch the icon accordingly.


You must set the AppUserModelID before your app shows any GUI. If you need to access other Windows 7 features you can have a look at Q7Goodies which is a Qt add-on for Windows 7 with a PyQt bindings.