How to set window icon with PyQt5?

from PyQt5 import QtWidgets, QtGui from PyQt5.QtWidgets import * from PyQt5.QtCore import * class Application(QMainWindow): def __init__(self): super(Application, self).__init__() self.setWindowIcon(QtGui.QIcon('icon.png')) 

I am trying to set the window icon (top left of the window), but instead the normal icon has disappeared.

I tried with many resolutions (8x8, 16x16, 32x32, 64x64) and extensions (.png and .ico).

What am I doing wrong?

+5
source share
2 answers

The answer was asked by a question (invisible icon). I wanted to add that the script may not be executed in the script directory. In any case, to be safe, you might want to make sure that the icon is loaded relative to the directory in which the script is located:

 import os # [...] scriptDir = os.path.dirname(os.path.realpath(__file__)) self.setWindowIcon(QtGui.QIcon(scriptDir + os.path.sep + 'logo.png')) 
+1
source

The command suggested by acer works for me:

  self.setWindowIcon(QtGui.QIcon('icon.png')) 

I installed 256x256 png and everything was fine. I have Win 7 pro 64 bit, Python 3.5.2 32 bit.

+1
source

Source: https://habr.com/ru/post/1265038/


All Articles