PySide is a free software. Unlike PyQt, it uses LGPL to allow PySide to develop commercial software for free.
PySide has 15 modules. These modules support operations on the GUI, multimedia, XML, network, and database. This tutorial aims to learn two modules: QtCore and QtGui.
QtCore is a non-interface module that provides support for file directories, data types, input and output streams, URLs, and threads. QtGui supports common interface elements, such as dialog box, window, Status Bar, and toolbar.
Start with Hello world
Copy codeThe Code is as follows:
#! /Usr/bin/python
# Import PySide classes
Import sys
From PySide. QtCore import *
From PySide. QtGui import *
# Create a Qt application
App = QApplication (sys. argv)
# Create a Label and show it
Label = QLabel ("Hello World ")
Label. show ()
# Enter Qt application main loop
App.exe c _()
Sys. exit ()
To compile a PySide desktop application, you must first import QtCore and QtGui, because these classes contain the main functions of the application. QtGui contains interface elements, and QtCore contains functions for processing signals and slots. After the import module is complete, you need to create a QApplication object. This object must accept parameters from the command line. Therefore, you need to pass sys. argv in QApplication. Then, in order to make the Hello world appear smoothly, A QLabel object is required. Finally, call the show method to display the label.
App.exe c _ () will enter the main cycle of Qt and continuously round the listening signal. After receiving the signal, it will look for and process the signal handle, that is, the slot function.
Sys. exit () means to exit.
Tips:
The last two sentences are generally written as sys.exit(app.exe c _())
In this case, it is written as app.exe c _ () but not app.exe c () Because exec is a keyword in python, so Qt uses exec _ to avoid it.
In addition, you can use html tags in PySide to enrich the content, for example:
Copy codeThe Code is as follows:
Label = QLabel ("<font color = red size = 40> Hello World </font> ")