Pyside Chapter 1: GUI Development in Python
What is PySide???
PySide is a Python binding of the cross-platform GUI development toolkit Qt, currently developed by the Qt company under the Qt for Python Project. Pyside provides LGPL-licensed Python bindings for the Qt 4. It also includes complete toolchain for rapidly generating bindings for any Qt-based C++ class hierarchies. PySide Qt bindings allow both free open source and proprietary software development and ultimately aim to support Qt platforms. Pyside is a really useful framework used for developing cool looking Graphical User Interfaces easily for your python applications. We would be using Pyside2, the latest version of Pyside.
Installing Pyside2
- Create a new conda environment using
conda create — name pyside
. This will create a new conda environment by the name pyside, which we will be using for installing the necessary dependencies. - Activate the environment created in the last step using
activate pyside
. In case you are on Linux, usesource activate pyside
. - Install PySide2 in this environment using
conda install -c conda-forge pyside2
. It would install all the necessary dependencies along with PySide2.
Introduction to Pyside2
We will start by creating a simple GUI application displaying a Hello World text on the interface. We will start by importing the necessary libraries.
While developing a PySide application, we would usually start by importing the PySide2.QtWidgets
classes since they have the main functions for implementing a Qt GUI in Python. Next we will start creating our application.
We start by creating an app
which is an instance of the PySide2.QtWidgets.QApplication
class. It manages the GUI applications control flow and main settings.
Next we will be creating a widget. A widget is the smallest unit of a user interface; it receives mouse, keyboard and other events from the window system, and paints a representation of itself which is rectangular in shape on the screen.
We create a widget wid
using PySide2.QtWidgets.QWidget
class and display it on the screen using the show()
method. The method app.exec_()
lets us enter the Qt main loop and start executing the Qt code. After execution of the Qt code, when we close the application, the program is terminated by sys.exit()
.
We can also change the title of the GUI application using the setWindowTitle()
method.
When we execute the above script, we get the following result:
Now we will try to display some text on the User Interface. This can be achieved using the PySide2.QtWidgets.QLabel
. While QWidget
creates a blank widget, QLabel
creates a widget that provides text or image display.
We create a label initialized with out display string “Hello World!”
. Then we resize the label widget and set a title for it and display it using show()
. The result on running this script will be:
As you can see that the display string is not only small in size but is also not properly aligned which surely does not make it look good. We can fix this by initializing the label with custom HTML instead of pain string.
The output this time will be:
For the next articles on PySide and more such articles, stay tuned on https://geekyrakshit.ml.