Last Updated on 2021-11-08 by Clay
PySide is a Python package for making graphical interfaces (GUI), but everyone who has the most impression should be PyQt and Tkinter.
I have used PyQt5 for a period of time, and left a related article: [PyQt5] Tutorial(1) Install PyQt5 and print "Hello World!", If you have interesting of it, feel free to read it.
If fact, the origins of PySide and PyQt are also Qt. The reason is that a lot of things have happened naturally. I won't go into details here for the conflict stories that can be used to make movies.
Simply put, PySide can be used for commercial purposes, and PyQt needs to purchase a license if it wants to make a profit.
In addition, the code compatibility between PySide and PyQt os extremely high; it is no exaggeration to say that you can even take the code from one side to the other side for smooth execution.
The sample code of PySide in this article is PySide6, which corresponds Qt6. At the moment when I write down this note (2021-08-09), it is not an exaggeration to say that it is the latest Python package for making graphical interfaces.
By the way, it currently only supports Python3.6+ and does not support Python3.10.
Download and Installation
I recommend to build a virtual environment, but you can skip this step.
(Optional)Build virtual environment
mkdir pyside6_demo
cd pyside6_demo
python3 -m venv venv
source venv/bin/activate
Installation
pip3 install pyside6
Sample Code
After installation, we need to test the PySide6 package can work.
Anytime when we learn a new programming language or framework, we can print Hello World as the first step.
# coding: utf-8
import sys
from PySide6.QtWidgets import QApplication, QLabel, QWidget
class MainWindow(QWidget):
def __init__(self):
# Window
super(MainWindow, self).__init__()
self.resize(200, 150)
self.setWindowTitle("PySide6 Demo")
# QLabel
self.label = QLabel(self)
self.label.setGeometry(65, 65, 100, 10)
self.label.setText("Hello World!")
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
sys.exit(app.exec_())
Output:
In my environment, PySide6 can run normally, great!
It is very interesting that I copied the code directly from my old PyQt5 project, and only changed the module name of PyQt5 to PySide6.
References
- https://medium.com/weekly-python/getting-started-writing-qt-6-applications-in-python-with-pyside6-389ee4c384ee
- https://www.reddit.com/r/QtFramework/comments/ni27qi/complete_26_part_pyside_tutorial_updated_for/