PySide6 Part 1
About the series
In this series i will cover the basics of PySide6 and how to use it to create a simple GUI Application in Python.
PySide6
PySide6 also known as Qt for Python is a cross-platform, open-source, GUI toolkit for Python.
PySide6 is the official binding for Qt on Python and is now developed by the Qt Company itself.
Getting Started With PySide6
In this tutorial we’ll learn how to use PySide6 to create Desktop applications in Python.
Creating an application
To create our first application, let’s create a python file called app.py and save it anywhere accessible.
and to use PySide6 we need to install it first.
python3 -m pip install PySide6
then we write a simple application in Python:
a simple PySide6 app should look like
from PySide6.QtWidgets import QApplication, QWidget
# import os module to access command line arguments
import os
# We need one (and only one) QApplication instance per application.
# Pass in sys.argv to allow command line arguments for your app.
# If you know you won't use command line arguments QApplication([]) is fine.
app = QApplication(sys.argv)
# Create a Qt Widget instance, witch will be the main window of our application
window = QWidget()
# And because the window is hidden by default, we need to show it
window.show()
# Start the event loop.
# You need to call this once your application is ready to receive events.
app.exec()
# Our application will run until quit() is called, which we do on the window being closed.
# So we won't actually get here until we close the window.
Now we have a simple application, we can run it by executing the following command:
python3 app.py
The application will run and you can see the window.
Walking through the code
Now let’s walk through the code and see how it works. so we understand what exactly is happening.
-
First we import the
QApplicationclass fromPySide6.QtWidgetswhich is the main class for creating applications, andQWidgetwhich is the base class for all user interface objects in Qt.from PySide6.QtWidgets import QApplication, QWidget -
The main modules for
QtareQtWidgets,QtGui,QtCoreand others. -
Next we create an instance of
QApplicationand pass in the command line argumentssys.argv, which is a list of strings containing the command line arguments passed to the script.app = QApplication(sys.argv) -
Next we create an instance of
QWidgetand set it as the main window of the application.window = QWidget() window.show()In
Qtall top level widgets are calledwindows. they don’t have a parent widget, and they are not children of any other widget.Windows without a parent are invisible by default. So we must always call
show()to make the window visible. If we don’t callshow()the window will not appear on the screen.show()is a method of theQWidgetclass that makes the widget visible.
The window holds our user-interface and is the main entry point for the application. Any application can have multiple windows, and each window can have multiple widgets.
-
Finally we call the
exec()method of theQApplicationinstance to enter the main event loop.app.exec()The
exec()method is a blocking call that will return when the application has been closed.
Enjoy Reading This Article?
-
Here are some more articles with similar tags:
-
Here are some more articles with similar content: