PySide2 is a Python API for the Qt framework.
This API is made with Shiboken2, the Python binding generator.
It means that you can write your code in Python and use the Qt framework as you’d do with C++.
Even building a GUI with the Qt Designer is possible.
So what do we wait to start using it?
We need to install Python 3 on your computer.
So in order to have the exact same software and libraries installed in the exact same locations, I suggest to follow the following tutorial:
Yes it's for Windows, but if you are using another OS I guess you'll be able to adapt it.
You are maybe wondering why PySide2 and not PySide?
It's because PySide is for Qt 4 and PySide2 for Qt 5.
Same thing with Shiboken2 (for Qt 5) and Shiboken (for Qt 4).
Now that you have Python on your OS, let's install PySide2 and Shiboken2.
It's actually quite easy, just open a console and type the following:
python -m pip install PySide2
You'll get this message:
Collecting PySide2 Downloading https://files.pythonhosted.org/packages/e9/0c/ 9574f74fa125bacb101dfde825f944 b567f838d48e56cf7ee02c7fe94e87 /PySide2-5.13.2-5.13.2-cp35. cp36.cp37-none-win_amd64.whl (122.1MB) |█████████████████████████████ ███| 122.2MB Collecting shiboken2==5.13.2 Downloading https://files.pythonhosted. org/packages/5a/c8/ 2b5c3e29755d862a504c8e225812d2 049964a047f3656684577e9e472d8a /shiboken2-5.13.2-5.13.2-cp35. cp36.cp37-none-win_amd64.whl (2.1MB) |█████████████████████████████ ███| 2.1MB Installing collected packages: shiboken2, PySide2 Successfully installed PySide2-5.13.2 shiboken2-5.13.2
You have now PySide2 and Shiboken2 ready to be used.
There are installed at this location:
Shiboken2 won't be used in this tutorial, indeed PySide2 comes already generated with all what we need.
To be more precise, Shiboken2 is like Boost.Python, that is a wrapper to generate API from C++ code and to be used with Python.
So we'll use here PySides2 directly without compiling or generating it.
Let's now test our new toys in order to check it everything is working as expected.
Unlike with C++, there is absolutely nothing to compile.
What you have to do is just to create a new file and write some lines of code.
So to stay synchronized with this tutorial let's create a Python file at this location:
A classic, our dear Hello World from PySide2:
To execute this file, open a console in this directory:
Then type in your console:
python BadprogHello.py
The Qt window should appear.
If yes then PySide and Qt framework are ready to go further on your computer.
OK, our Hello World example was very rudimentary.
Lets' try to create a window from the Qt Designer and use it with Python.
As you installed Qt framework (with PySides2) you have also installed the Qt Designer.
Open the following file by double clicking it:
The Qt Designer is now open.
An important note here, this Qt Designer is exactly the same as the one used with C++.
So don't worry, the final result will be exactly the same with Python.
Let's create a new file.
From Qt Designer > File > New > Main Window > Create.
Saved this file in the following directory:
And give it the name: BadprogWindow.ui
So you have now this new file on your computer:
Let’s add some Widgets on this window (from the left pane of Qt Designer) by just clicking and moving them onto the BadprogWindow.ui.
For this example I added a Push Button and a Label.
So their respective name are by default pushButton and label.
Let is like that and save this file.
Here the code of this BadprogWindow.ui file:
As you can see this UI file is an XML one.
So how to tell Python that we want this XML file becoming a GUI one?
It’s where PySide2 will help us by providing some useful tools such as:
To use it you have to open it from a console.
So open a console in the following location (where you saved your UI file):
And type this command directly in your console:
pyside2-uic.exe BadprogWindow.ui > BadprogWindow.py
Here we are saying to PySide2 to create a new Python file BadprogWindow.py from the UI file.
Now open this new file generated:
This time it's a Python code.
This file contains a class:
And 2 methods:
We'll use the first method (setupUi) in our example below.
Let's create now a third file called BadprogMain.py for our main code:
Here we are, we have now our 3 files ready to be used.
So if you made some modifications on your BadprogWindow.ui file, you'll have to regenerate the BadprogWindow.py.
Otherwise modifications won't be applied.
Then simply execute your third file BadprogMain.py directly from its directory:
Open a console then type:
python BadprogMain.py
The window from Qt framework is displayed with a PushButton and a Label.
Click this PushButton to see the text Label change from TextLabel to Hello from Badprog :D.
If you are a C++ and Qt user you certainly found this example very pleasant to setup.
And if you are a Python programmer, you probably discovered how to create your first Window with Qt and PySide2.
Anyway, good job, you did it.
Comments
tj (not verified)
Sunday, August 9, 2020 - 6:52pm
Permalink
Thank you :) very clear
Thank you :) very clear explanation. Was a pleasure to read.
js (not verified)
Friday, September 23, 2022 - 12:40am
Permalink
Very great tutorial! Thank
Very great tutorial! Thank you!
Add new comment