33

I'm working on an applcation in Python's PyQt4 and cannot find how to change the taskbar icon. I made my .ui files in Qt's Designer, where I can change the windowIcon properties. But that is not what I am looking for. I want to change the look of the application's icon in windows taskbar. For now it is Python logo in a window icon.

I found some information on SO: link but it's not helping me much.

I tried:

app = QtGui.QApplication([])
app.setWindowIcon(QtGui.QIcon('chip_icon_normal.png'))
app.exec_()

But the icon remains unchanged.

What i want to change, showing the picture: (This is done calling the setWindowIcon on main window/ dialog, or the application, as shown above.)

Taskbar icon change

ekhumoro
  • 98,079
  • 17
  • 183
  • 279
Marko
  • 19,347
  • 13
  • 45
  • 61
  • What platform are you on? And why doesn't that SO link help you? – ekhumoro Sep 16 '12 at 23:46
  • If you are not looking for the icon defined by `windowIcon`, I'm not sure which icon you are looking for. A screenshot would help. – Avaris Sep 18 '12 at 07:37
  • @ekhumoro I have Python 2.7 on Windows 8 64bit, using PyQt4. And that link doesn't help me because it changes the window left top corner icon, not the one in the taskbar, as shown in the edited post. – Marko Sep 20 '12 at 23:58
  • @Marko. But did you read the [accepted answer](http://stackoverflow.com/a/1552105/984421) and try the suggested workaround? – ekhumoro Sep 21 '12 at 00:18
  • @ekhumoro Thanks and brilliant. I didn't read it well beforehand! Please submit an answer, so I can award you the bounty! – Marko Sep 21 '12 at 00:27
  • 1
    @Marko: Alternatively, if you convert your app to an `exe` with its own icon (with `py2exe` or `pyinstaller`), that also solves the issue. Just a heads up, if you were planning on doing this in the end. – Avaris Sep 21 '12 at 00:43
  • @Avaris Infact that is something that I am planning to do. What you said, was very useful, and if I understand it means, if I am going to make an exe file, I can freely remove those added lines? And it's is like somekind of a "bug" or something. – Marko Sep 21 '12 at 00:47
  • @Marko: Yes, you can leave out those lines for the `exe` package. This issue can be considered as a bug, probably solvable in the `pythonw.exe` side. That answer explains it quite well actually. – Avaris Sep 21 '12 at 01:01

4 Answers4

25

This problem is caused by some peculiarities in how taskbar icons are handled on the Windows platform.

See this answer for details, along with a workaround using ctypes.

Community
  • 1
  • 1
ekhumoro
  • 98,079
  • 17
  • 183
  • 279
16

It seems to me that the problem may be caused by lack of icon with the right size. The following setup worked for me in PyQT4:

# set app icon    
app_icon = QtGui.QIcon()
app_icon.addFile('gui/icons/16x16.png', QtCore.QSize(16,16))
app_icon.addFile('gui/icons/24x24.png', QtCore.QSize(24,24))
app_icon.addFile('gui/icons/32x32.png', QtCore.QSize(32,32))
app_icon.addFile('gui/icons/48x48.png', QtCore.QSize(48,48))
app_icon.addFile('gui/icons/256x256.png', QtCore.QSize(256,256))
app.setWindowIcon(app_icon)

I have got a task bar icon in Windows 7 and correct icons in all windows without any changes to ui files.

Slawek Rewaj
  • 683
  • 6
  • 15
4

You need to call setWindowIcon(...) on the window, not on the application.

Here's an example, which works for me:

#!/usr/bin/env python3

import os
import sys
import subprocess
import os.path

from PyQt4 import QtGui
from PyQt4 import QtCore

class MyWin(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MyWin, self).__init__(parent)
        self.setWindowTitle("My Window")
        self.setWindowIcon(QtGui.QIcon('test_icon.png'))
        self.show()

def main(args):
    app = QtGui.QApplication([])

    ww= MyWin()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main(sys.argv[1:])
Jeremiah
  • 1,369
  • 7
  • 15
  • 2
    No, setting an icon for `QApplication` is fine. It defines the default icon. Any window that doesn't specify an icon will use that. [See docs](http://doc.qt.nokia.com/4.7-snapshot/qwidget.html#windowIcon-prop) – Avaris Sep 15 '12 at 01:46
  • @Jeremiah i don't know if it would work, because i initialize my code differently, but i think that you do the same, with setWindowIcon method, but it's the small icon in the left top corner of the window if i'm not mistaken? – Marko Sep 15 '12 at 13:45
  • Actually, my answer won't solve the problem. I just now tried removing the self.setWindowIcon(...) in the __init__() method, and called app.setWindowIcon(...) inside of main instead, and it works, as Avaris said. I have no explanation why it would work for me, but not for Marko. – Jeremiah Sep 17 '12 at 17:06
3

For me, the following code works for both changing task bar icon and window icon

win.setWindowIcon(QIcon('logo.png'))
  • 3
    Welcome to StackOverflow! You are answering a very old question which already has an answer. The OP has already tried your code, but it wasn't working in his case. – Kamyar Infinity Apr 21 '17 at 06:07
  • @user6657441 Same as Kamyar, except you are right for PyQt5 it now handles it cross platform using `setWindowIcon()` – melMass Jul 24 '17 at 23:27