Pyside2 Window Icon | Python GUI – In this Pyside2 GUI Development article i want to show you how to create Window Icon.
so before this we had some articles on Pyside2 GUI Development, you can check them in the below link.
Check Python GUI Development With Pyside2
1: Getting Started With Pyside2 | Qt For Python
2: Pyside2 GUI Creating First Window
So now this is the complete code for Pyside2 Window Icon | Python GUI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
from PySide2.QtWidgets import QApplication, QWidget import sys from PySide2.QtGui import QIcon class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Setting Icon") self.setGeometry(300,300, 500,400) self.setIcon() def setIcon(self): appIcon = QIcon("icon.png") self.setWindowIcon(appIcon) myApp = QApplication(sys.argv) window = Window() window.show() myApp.exec_() sys.exit(0) |
So in the above code first we have imported our classes from Pyside2 library
1 2 3 |
from PySide2.QtWidgets import QApplication, QWidget import sys from PySide2.QtGui import QIcon |
And this is our main window class that inherits from QWidget, in that class we are going to add the window title , window geometry and minimum width and height for the window.
also you can check Python Object Oriented Programming Articles in the link. Python Object Oreinted Programming.
also we have added the setIcon() method in this class.
1 2 3 4 5 6 7 8 |
class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Setting Icon") self.setGeometry(300,300, 500,400) self.setIcon() |
This is the method that we are going to set our icon, make sure that you have an icon in your working directory, as i have already added an icon to
my working directory. so in here first you need to create the QIcon object and after that you set the window icon by using setWindowIcon().
1 2 3 |
def setIcon(self): appIcon = QIcon("icon.png") self.setWindowIcon(appIcon) |
So in here every PyQt5 application must create an application object. The sys.argv
parameter is a list of arguments from a command line.
Python scripts can be run from the shell. It is a way how we can control the startup of our scripts.
1 |
myApp = QApplication(sys.argv) |
In here we create the object of our main window class.
1 |
window = Window() |
Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. The mainloop ends if we call the exit()
method or the main widget is destroyed. The sys.exit()
method ensures a clean exit. The environment will be informed how the application ended.
The exec_()
method has an underscore. It is because the exec
is a Python keyword. And thus, exec_()
was used instead.
1 2 |
myApp.exec_() sys.exit(0) |
So now run the complete code and this will be the result, a window with an icon.
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email