Python runs the first pyside2 form program

Time:2021-9-13

The previous chapter introduced the installation of pyside2 and how to start the program for page design, and integrated the tool into the pycharm extension tool. There are two wrong places. The pyuic tool is used, which needs to be changed to pyside2-uic.exe. Specific changes:

The configuration in pycharm extension tool also needs to be adjusted:

The configuration in the previous article is pyqt5, and pyside2 is mainly used for learning here.

After modifying to the correct configuration, select the UI file with the mouse and right-click pyside2 UIC in the extension tool to convert it to Python script.

Let’s take a look at a simple GUI page I drew:

After saving the page file, the suffix is in. UI format. If you open it with a text file, the content is in XML format:

Postman.ui source code:


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>948</width>
    <height>617</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget name="comboBox">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>30</y>
     <width>81</width>
     <height>31</height>
    </rect>
   </property>
   <item>
    <property name="text">
     <string>GET</string>
    </property>
   </item>
   <item>
    <property name="text">
     <string>POST</string>
    </property>
   </item>
  </widget>
  <widget name="lineEdit">
   <property name="geometry">
    <rect>
     <x>170</x>
     <y>30</y>
     <width>541</width>
     <height>31</height>
    </rect>
   </property>
  </widget>
  <widget name="pushButton">
   <property name="geometry">
    <rect>
     <x>740</x>
     <y>30</y>
     <width>151</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text">
    <string>Send</string>
   </property>
  </widget>
  <widget name="label">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>90</y>
     <width>72</width>
     <height>15</height>
    </rect>
   </property>
   <property name="text">
    <string>Params</string>
   </property>
  </widget>
  <widget name="label_2">
   <property name="geometry">
    <rect>
     <x>160</x>
     <y>90</y>
     <width>121</width>
     <height>21</height>
    </rect>
   </property>
   <property name="text">
    <string>Headers</string>
   </property>
  </widget>
  <widget name="textEdit">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>150</y>
     <width>821</width>
     <height>331</height>
    </rect>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
 

Python script after conversion: postman.py


# -*- coding: utf-8 -*-
 
 
################################################################################
## Form generated from reading UI file 'postman.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
 
 
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
 
 
 
 
class Ui_Dialog(object):
    def setupUi(self, Dialog):
        if not Dialog.objectName():
            Dialog.setObjectName(u"Dialog")
        Dialog.resize(948, 617)
        self.comboBox = QComboBox(Dialog)
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.setObjectName(u"comboBox")
        self.comboBox.setGeometry(QRect(70, 30, 81, 31))
        self.lineEdit = QLineEdit(Dialog)
        self.lineEdit.setObjectName(u"lineEdit")
        self.lineEdit.setGeometry(QRect(170, 30, 541, 31))
        self.pushButton = QPushButton(Dialog)
        self.pushButton.setObjectName(u"pushButton")
        self.pushButton.setGeometry(QRect(740, 30, 151, 31))
        self.label = QLabel(Dialog)
        self.label.setObjectName(u"label")
        self.label.setGeometry(QRect(70, 90, 72, 15))
        self.label_2 = QLabel(Dialog)
        self.label_2.setObjectName(u"label_2")
        self.label_2.setGeometry(QRect(160, 90, 121, 21))
        self.textEdit = QTextEdit(Dialog)
        self.textEdit.setObjectName(u"textEdit")
        self.textEdit.setGeometry(QRect(70, 150, 821, 331))
 
 
        self.retranslateUi(Dialog)
 
 
        QMetaObject.connectSlotsByName(Dialog)
    # setupUi
 
 
    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None))
        self.comboBox.setItemText(0, QCoreApplication.translate("Dialog", u"GET", None))
        self.comboBox.setItemText(1, QCoreApplication.translate("Dialog", u"POST", None))
 
 
        self.pushButton.setText(QCoreApplication.translate("Dialog", u"Send", None))
        self.label.setText(QCoreApplication.translate("Dialog", u"Params", None))
        self.label_2.setText(QCoreApplication.translate("Dialog", u"Headers", None))
    # retranslateUi

The above two scripts alone cannot be run, and a few lines of code need to be written separately to load the page window for display:

run_postman.py:

import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from postman import Ui_Dialog
 
 
if __name__ == "__main__":
    #Create an application object
    app = QApplication(sys.argv)
    #Create a form object
    MainWindow = QMainWindow()
    
    ui = Ui_Dialog()
    ui.setupUi(MainWindow)
    #Setting window display
    MainWindow.show()
    sys.exit(app.exec_())

The effect after operation is shown in the following figure:

If you are interested, you can adjust the page design according to your preferences and realize your own test gadgets.

This is the end of this article about Python running the first pyside2 form program. For more information about Python running the first pyside2 form program, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!