1
votes

I study this tutorial http://zetcode.com/gui/pyqt5/firstprograms/ and code here.

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

        self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('Icon')
        self.setWindowIcon(QIcon('web.png'))        

        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())  

Can I show Window icon? I use PyQt5, Python3.4 and Linuxmint 17.2 cinnamon.

This Window

4

4 Answers

3
votes

i am not sure, what you expected: the windowicon is shown as applicationIcon (e.g. here on ubuntu gnome, see my code in the background)

enter image description here

If no icon has been set, windowIcon() returns the application icon

(QApplication::windowIcon()).

http://doc.qt.io/qt-5/qwidget.html#windowIcon-prop

looks on ubuntu like this:

enter image description here

so i think, the code is working correctly

1
votes

Check where the icon file is placed. It should be placed in the execution directory in your case. You can check the execution directroy with the following change:

if __name__ == '__main__':
    import os
    print("Work dir:" + os.getcwd())

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())  
1
votes

I faced the exact same problem.

First things first. There is no setWindowIcon() method under QWidget or QMainWindow classes, in fact. you should be trying to set the QIcon at the Application level as follows.

app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('home.png'))

Second, the icon thus created using this code does not reflect on the title of the window, instead it will reflect as an application icon as shown in the image below. the home.png

Application icon in Mac not the Window Icon

Finally, the path does not really matter, it can be an absolute path or a relative path, the system will consider either.

0
votes

try PySide.QtGui.QApplication.setWindowIcon(icon). Like this:

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon('web.png'))
    ex = Example()
    sys.exit(app.exec_())