HumanAfterAll
HumanAfterAll

Reputation: 351

How to set a 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 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?

Upvotes: 25

Views: 73739

Answers (5)

Yokozuna
Yokozuna

Reputation: 113

from PyQt5.QtGui import QIcon

self.setWindowIcon(QIcon('Notepad_Vista_10.png'))

Upvotes: 0

Yassin Mohammed
Yassin Mohammed

Reputation: 25

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'))        

Upvotes: 0

Santanu Pal
Santanu Pal

Reputation: 31

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)

Upvotes: 1

drgrujic
drgrujic

Reputation: 441

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.

Upvotes: 16

DomTomCat
DomTomCat

Reputation: 8569

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'))

Upvotes: 11

Related Questions