How to set icon to a window in PyQt5 ?

When we design a PyQt5 application we see an icon on the top left corner, by default it looks like this : 
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 librariesfrom PyQt5.QtWidgets import *from PyQt5 import QtCorefrom PyQt5 import QtGuiimport sysclass 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 appApp = QApplication(sys.argv)# create the instance of our Windowwindow = Window()# start the appsys.exit(App.exec()) |
Output :



