Home > Article > Backend Development > Python server programming: developing GUI programs using PySide2
Python has become a very popular programming language, and many developers take advantage of Python's simplicity and flexibility to create a variety of applications. Among them, Python is also used to develop server-side applications, and PySide2 is one of the most important tools in this process.
PySide2 is a binding for the Python programming language for the Qt framework. This tool allows Python programmers to write programs that use GUI structures. Because PySide2 is a cross-platform tool that runs on all major operating systems, it is particularly suitable for developing server-side programs.
In this article, we will discuss how to develop GUI programs using PySide2 and Python server programming. We'll start with simple PySide2 applications, show how to add functionality and event handlers to them, and discuss how to create GUI programs with complex features such as UI controls and animations.
1. Install PySide2
First, we need to install PySide2. PySide2 runs on different operating systems, so install accordingly depending on your operating system.
On Linux, we can install PySide2 through pip using the following command:
pip install PySide2
On Windows systems, we can use the same command in the command prompt to install PySide2.
After the installation is complete, we can start writing our first PySide2 application.
2. Develop a simple PySide2 application
We will start with a very simple PySide2 application that only creates a window and a tab. In order to create this application, we need to import the PySide2.QtWidgets and sys modules:
import sys from PySide2.QtWidgets import QApplication, QLabel, QWidget
Next, we need to create a QApplication and QWidget object. We will use QWidget objects to create our GUI:
app = QApplication(sys.argv) widget = QWidget() widget.setWindowTitle('My First PySide2 Application') widget.setGeometry(300, 300, 250, 150) label = QLabel('Hello World', widget) label.move(100, 50) widget.show() sys.exit(app.exec_())
This code snippet will create a QWidget object and set its position and size. It will also create a label and add it to the QWidget object. Labels are created using the QLabel class. Finally, we use the show method to display the GUI and exit the application.
3. Add event handlers to the application
Now, we have created a simple PySide2 application, but it can't do anything yet. To make our application react to events, we need to add event handlers.
In PySide2, we can use slots to define event handlers. Slots are a method for receiving GUI events and handling them. In this example, we will create a new class that will inherit QWidget and define a slot method.
The code snippet below will create a button and add it to the QWidget object. This button will trigger the PushButtonClicked event. This event will be fired whenever the button is clicked. We will define a slot method in our application that will handle the PushButtonClicked event and display a message on the console:
import sys from PySide2.QtCore import Slot from PySide2.QtWidgets import QApplication, QPushButton, QWidget class MyWidget(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): button = QPushButton('Click me', self) button.setToolTip('Click this button') button.move(100, 70) button.clicked.connect(self.on_pushButton_clicked) self.setGeometry(300, 300, 250, 150) self.setWindowTitle('My Second PySide2 Application') self.show() @Slot() def on_pushButton_clicked(self): print('Button clicked.')
Now when we click the button, the program will display it on the console "Button clicked" message. Note that the @Slot() decorator is used in front of the method. This tells the program that this method is a PySide2 slot to be called.
4. Create a GUI program with UI controls and animations
Now we can create a simple PySide2 application with buttons and event handlers. Next, let's add more controls and animations to the program.
In this example, we will create a new class that will inherit the QDialog class. QDialog is a class in PySide2 used to display dialog boxes. In this class, we will create a new tag for the sunflower to track the light source, and use PySide2's animation framework to create a small sun. We will also add a button to control the movement of the sun.
import sys from PySide2.QtCore import QPropertyAnimation, QRect from PySide2.QtGui import QPainter, QColor from PySide2.QtWidgets import QApplication, QDialog, QLabel, QPushButton from PySide2 import QtGui class Sun(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setFixedSize(100, 100) self.color = QColor(255, 255, 0) def paintEvent(self, event): painter = QPainter(self) painter.setBrush(QtGui.QBrush(self.color)) painter.drawEllipse(0, 0, self.width(), self.height()) class FollowSunDialog(QDialog): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('Follow the Sun') self.setGeometry(100, 100, 400, 400) sun_label = QLabel(self) sun_label.setFixedSize(50, 50) pixmap = QtGui.QPixmap('sun.png') sun_label.setPixmap(pixmap) self.sun = Sun() self.sun.move(50, 150) self.button = QPushButton('Start', self) self.button.move(50, 300) self.button.clicked.connect(self.onClick) def onClick(self): animator = QPropertyAnimation(self.sun, b'geometry') animator.setDuration(3000) animator.setStartValue(QRect(50, 150, 100, 100)) animator.setEndValue(QRect(200, 50, 50, 50)) animator.start() if __name__ == '__main__': app = QApplication(sys.argv) dialog = FollowSunDialog() dialog.show() sys.exit(app.exec_())
In this code snippet, we define two classes, the Sun class and the FollowSunDialog class. The Sun class is a subclass of QWidget and is used to draw images of the sun. The FollowSunDialog class is a subclass of QDialog and is used to display dialog boxes and control the movement of the sun.
We also added a sun image for drawing the small sun, which is a sun.png file stored in the same directory as the program file. We created a QPushButton that when pressed will start the animation of the sun widget moving towards the upper right corner.
The animation is created using the PySide2 animation framework. When the button is pressed, we define a QPropertyAnimation object that uses the startValue and endValue properties to define the starting and ending positions of the sun widget. It also defines the timing of the animation.
Conclusion:
This article introduces the combination of PySide2 and Python server programming. We created a simple PySide2 application, added event handlers to the application, and created a UI GUI program for controls and animations. PySide2 is a very powerful tool for creating Python applications with GUI structure. For server-side developers, PySide2 can reduce development time and increase application interactivity and ease of use.
The above is the detailed content of Python server programming: developing GUI programs using PySide2. For more information, please follow other related articles on the PHP Chinese website!