When we design a PyQt5 application we see an icon on the top left corner, by default it looks like this : In this tutorial, we will see how to change the icon according to the user need, in order to do this we use setWindowIcon() method and to load the icon QIcon will be used which belongs to QtGui class.
Syntax : setWindowIcon(QtGui.QIcon(‘icon.png’)) Argument : It takes file name if file is in same folder else file path.
Code :Â
Python3
# importing the required libraries Â
from PyQt5.QtWidgets import * from PyQt5 import QtCore from PyQt5 import QtGui import sys Â
Â
class Window(QMainWindow): Â Â Â Â def __init__( self ): Â Â Â Â Â Â Â Â super ().__init__() Â
        self .setWindowIcon(QtGui.QIcon( 'logo.png' ))         # set the title         self .setWindowTitle("Icon") Â
        # setting the geometry of window         self .setGeometry( 0 , 0 , 400 , 300 ) Â
        # creating a label widget         self .label = QLabel("Icon is set ", self ) Â
        # moving position         self .label.move( 100 , 100 ) Â
        # setting up border         self .label.setStyleSheet("border: 1px solid black;") Â
Â
Â
        # show all the widgets         self .show() Â
Â
# create pyqt5 app App = QApplication(sys.argv) Â
# create the instance of our Window window = Window() Â
# start the app sys.exit(App. exec ()) |
Output :