Application icon in PySide GUI
Asked Answered
G

4

11

I have a PySide GUI app (written in Python 3, running on Windows 7 Pro) in which I’m setting the application icon as follows:

class MyGui(QtGui.QWidget):
    def __init__(self):
        super(MyGui, self).__init__()
        ...
        self.setWindowIcon(QtGui.QIcon('MyGui.ico'))

        if os.name == 'nt':
            # This is needed to display the app icon on the taskbar on Windows 7
            import ctypes
            myappid = 'MyOrganization.MyGui.1.0.0' # arbitrary string
            ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
        ...

I got that ctypes stuff from this answer. If I remove those lines then the Python icon is displayed in the taskbar when I execute python MyGui.py.

With those lines included everything looks great, with the correct icon on the window and the taskbar. However, when I package the gui using cxfreeze both the window and taskbar icons change to the generic windows .exe icon.

I’m using cxfreeze.bat to package the app, using the instructions found here, including the --icon switch. Using that switch makes the generated exe have the right icon when viewed in explorer. However, the application window, and taskbar, don’t show the icon when I launch the app. I’ve tried copying the .ico file to the same directory as the .exe but that doesn’t help.

I get the same behavior on both Windows 7 & 8. The curious thing is that if I pin the app to the taskbar, the taskbar icon is displayed correctly, but the window icon is still the generic exe icon.

How do I get the icon to display correctly?

Gilman answered 12/6, 2013 at 14:22 Comment(0)
N
8

PySide needs access to a special DLL to read .ico files. I think it's qico4.dll.

You could try changing the call to setWindowIcon to open the icon as a .png and put a .png of it in the ./dist directory and see if that works. If so, then your code is fine and I'm pretty sure it's the .dll problem. You'll need to tell cx_freeze to include the dll in the build.

I think PySide provides the embedded .ico to Windows and doesn't need to be able to read the data itself, so that's why this is working. However to read either the embedded icon resource or the ico file in the executable directory, it'll need the DLL.

Nickolas answered 12/6, 2013 at 15:48 Comment(1)
That works! Thank you so much. I did see some info talking about the qico4.dll requirement, but the solutions on where to put this were varied, and the couple that I tried didn't work. Having the icon as both .ico and .png is not the best, but it is the easiest solution to the problem.Gilman
G
6

I found another solution that doesn't require having the icon in both PNG and ICO formats. As Simon mentions in his answer, qico4.dll is required to read the .ico files. Also, this file needs to be placed in a directory named imageformats that is a subdirectory of your app directory. The folder structure should look like this:

My Gui
|
|-- MyGui.exe
|-- QtCore4.dll
|-- QtGui4.dll
|-- ...
|
|-- imageformats
    |
    |-- qico4.dll

qico4.dll is installed with your PySide distribution. If you pick typical installation options the file should be under

os.path.join(os.path.dirname(sys.executable), 
             'Lib',
             'site-packages',
             'PySide',
             'plugins',
             'imageformats' )
Gilman answered 13/6, 2013 at 15:8 Comment(0)
S
4

I was having the same problem and this is how I solved it. I had the icon in a png file (but I guess it also works with other formats.

First in Linux I dumped the image file content into a file with the following:

hexdump -v -e '"\\x" 1/1 "%02x" ' icon.png > icon.py

Then I edited icon.py to have the following format:

icon = b'hexdump'

e.g.

icon = b'\xf3\a3'

Then I imported the icon with:

from icon import icon

Then I set the icon with the following:

qp = QtGui.QPixmap()
qp.loadFromData(icon)
appIcon = QtGui.QIcon(qp)
self.setWindowIcon(appIcon)

And it worked. I think this is the most robust way. cx_Freeze can't interfere with it.

Swampy answered 17/11, 2020 at 17:59 Comment(1)
I had to modify the syntax slightly: hexdump -v -e '"\\\x" 1/1 "%02x" ' icon.png > icon.py worked for meBort
F
1

Based on Saren Tasciyan's answer, here's a cross-platform solution:

>>> from pathlib import Path
>>> hex_content = Path('icon.png').read_bytes()
>>> Path('icon.py').write_text(f'icon = {hex_content}')

And then just:

from icon import icon

pixmap = QtGui.QPixmap()
pixmap.loadFromData(icon)
appIcon = QtGui.QIcon(pixmap)
self.setWindowIcon(appIcon)
Flambeau answered 23/1, 2022 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.