Skip to content
Related Articles

Related Articles

Improve Article
How to set icon to a window in PyQt5 ?
  • Difficulty Level : Basic
  • Last Updated : 26 Mar, 2020

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 :bIt takes file name if file is in same folder else file path.

Code :

# importing the required libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore
from PyQt5 import QtGui
import sys
class Window(QMainWindow):
    def __init__(self):
        # set the title
        # 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
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output :

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :