24
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 a window icon (top left of the window) but the normal icon disappeared instead.

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

What am I doing wrong?

HumanAfterAll
  • 341
  • 1
  • 2
  • 6
  • 1
    Use an absolute path. – ekhumoro Mar 05 '17 at 00:27
  • Thank you but it's not working. I tried with self.setWindowIcon(QtGui.QIcon('C:/Users/John/PycharmProjects/pythonproject/icon.png')). Also, I had to add QtGui in the import to get QIcon. I am not even sure it is the good way to do it with PyQt5. – HumanAfterAll Mar 05 '17 at 00:44
  • 2
    Oh I found my mistake, I thought my icon was white on black, but in fact it was white on transparent. Everything is working right with self.setWindowIcon(QtGui.QIcon('icon.png')) . My bad, Thank you very much for the help ekhumoro! – HumanAfterAll Mar 05 '17 at 01:33

5 Answers5

16

The command, as suggested by asker, works for me:

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

I put 256x256 png and all was OK. I have Win 7 pro 64 bit, Python 3.5.2 32 bit.

drgrujic
  • 431
  • 5
  • 10
10

The answer has been given by the asker (invisible icon). I wanted to add that the script may not be executed in the script directory. In any case, to be safe, you may want to make sure the icon is loaded relative to the directory in which the script resides:

import os 
# [...]
scriptDir = os.path.dirname(os.path.realpath(__file__))
self.setWindowIcon(QtGui.QIcon(scriptDir + os.path.sep + 'logo.png'))
DomTomCat
  • 8,189
  • 1
  • 49
  • 64
1

I'm using PyQT5. And code should be as...

icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("programmer.png"), QtGui.QIcon.Selected, QtGui.QIcon.On)
MainWindow.setWindowIcon(icon)
Mitchell van Zuylen
  • 3,905
  • 4
  • 27
  • 64
  • I'm always receiving " QPixmap: Must construct a QGuiApplication before a QPixmap ". – Ben Oct 06 '22 at 14:59
0
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'))        
RafalS
  • 5,834
  • 1
  • 20
  • 25
  • I'm always receiving " QPixmap: Must construct a QGuiApplication before a QPixmap ". – Ben Oct 06 '22 at 14:59
0
from PyQt5.QtGui import QIcon

self.setWindowIcon(QIcon('Notepad_Vista_10.png'))
Friedrich
  • 2,011
  • 2
  • 17
  • 19
Yoko Abi
  • 3
  • 3
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Friedrich Apr 14 '23 at 15:33
  • I agree with @Friedrich... your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Without more explanation I will recommend deletion of your answer. – Rich Lysakowski PhD Apr 16 '23 at 04:28
  • How tf is this unclear its literally the answer its 1 line of Code that solves this issue. How is Santanu Pal`s answer not unclear then – Yoko Abi Apr 17 '23 at 05:14