pythonpyqtpyqt5

How to set application icon in PyQt5 on MacOS


I am making an app in python with PyQt5 on a MacOS computer.

This is my code:

from PyQt5.QtWidgets import QApplication, QWidget, QLabel

app = QApplication([])

window = QWidget()
window.setWindowTitle('Alarm App')

label = QLabel('Alarm App', window)

label.move(100, 50)
window.show()

app.exec_()

When I run the app the window appears and and I can see the icon on the dock, the icon is the python launcher icon which is not icon I want, in the directory of the project I have an icon.png.

I have tried:

window.setWindowIcon(QIcon("icon.png"))

But the icon still looks like this:

Python Launcher Icon


Solution

  • You need to use app.setWindowIcon instead of window.setWindowIcon. The following code works and does what you expect:

    from PyQt5.QtWidgets import QApplication, QWidget, QLabel
    from PyQt5.QtGui import QIcon
    
    app = QApplication([])
    
    # Using app instead of window to set the icon
    app.setWindowIcon(QIcon("icon.png"))
    
    window = QWidget()
    window.setWindowTitle('Alarm App')
    
    label = QLabel('Alarm App', window)
    
    label.move(100, 50)
    window.show()
    
    app.exec_()