Pyside2 Create Window | Python GUI – in this Pyside2 article i want to show you how to Create Window, so before this we had an introductory video to Pyside2, you can read that article in the below link.
Check Python GUI Development With Pyside2
1: Getting Started With Pyside2 | Qt For Python
Qt For Python (Pyside2)
Qt for Python offers Python bindings for Qt, enabling the use of Qt5 APIs in Python applications. It lets Python developers utilize the full potential of Qt, using the PySide2 module.
The PySide2 module provides access to the individual Qt modules such as QtCore, QtGui, and so on. Qt for Python also comes with the Shiboken2 CPython binding code generator, which can be used to generate Python bindings for your C or C++ code.
So now this is the complete code for Pyside2 Create Window | 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 |
from PySide2.QtWidgets import QApplication, QWidget import sys import time class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Pyside2 Simple Appplication") self.setGeometry(300,300, 500,400) self.setMinimumHeight(100) self.setMinimumWidth(250) self.setMaximumHeight(200) self.setMaximumWidth(800) myApp = QApplication(sys.argv) window = Window() window.show() time.sleep(3) window.resize(600,400) #window.repaint() myApp.exec_() sys.exit(0) |
So in the above code first we have imported the required classes from Pyside2 for creating window
1 2 3 |
from PySide2.QtWidgets import QApplication, QWidget import sys import time |
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 Oreinted Programming Articles in the link. Python Object Oreinted Programming
1 2 3 4 5 6 7 8 9 10 |
class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Pyside2 Simple Appplication") self.setGeometry(300,300, 500,400) self.setMinimumHeight(100) self.setMinimumWidth(250) self.setMaximumHeight(200) self.setMaximumWidth(800) |
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() |
This is for dynamically resizing the window
1 2 3 |
time.sleep(3) window.resize(600,400) #window.repaint() |
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 run the complete code and this will be the result
Also you can watch the video for this article
Subscribe and Get Free Video Courses & Articles in your Email