Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 479
HumanAfterAll Avatar asked Mar 04 '17 23:03

HumanAfterAll


People also ask

How do I add an icon to QT?

This can be done using Microsoft Visual Studio: Select File >> New, and choose the Icon File. Note: You need not load the application into the Visual Studio IDE as you are using the icon editor only. Store the ICO file in your application's source code directory, for example, with the name appico. ico .

How do I add an image to PyQt?

From the property editor dropdown select "Choose File…" and select an image file to insert. As you can see, the image is inserted, but the image is kept at its original size, cropped to the boundaries of the QLabel box. You need to resize the QLabel to be able to see the entire image.

What is a widget in PyQt?

Widgets are the basic building blocks for graphical user interface (GUI) applications built with Qt. Each GUI component (e.g. buttons, labels, text editors) is a widget that is placed somewhere within a user interface window, or is displayed as an independent window.


2 Answers

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.

like image 192
drgrujic Avatar answered Oct 05 '22 16:10

drgrujic


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'))
like image 35
DomTomCat Avatar answered Oct 05 '22 18:10

DomTomCat