How to set icon to a window in PyQt5?

How to set icon to a window in PyQt5?

In PyQt5, setting an icon to a window is done using the setWindowIcon() method of the QMainWindow or any QWidget object. Here's how you can do it:

  • Import necessary modules:
import sys from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtGui import QIcon 
  • Create the main application window and set its properties:
class App(QMainWindow): def __init__(self): super().__init__() self.title = 'PyQt5 Window Icon Example' self.left = 100 self.top = 100 self.width = 400 self.height = 300 self.initUI() def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) # Set window icon self.setWindowIcon(QIcon('path_to_icon.ico')) self.show() 

Replace 'path_to_icon.ico' with the path to your .ico file.

  • Run the application:
if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_()) 

Make sure to have the .ico icon file available at the specified path. You can also use other image formats like .png as the window icon. When specifying the path, you can use either an absolute path or a relative path depending on where your script is in relation to the icon file.


More Tags

single-page-application cloud guid bootbox udp node-webkit dialogfragment uppercase spring-cloud-config windows

More Programming Guides

Other Guides

More Programming Examples