[Summary of Liver Soul One Night The most complete and the most detailed on the whole network] Teach you the installation and use of PyQt5 step by step ☀️《❤️Remember to collect ❤️》

Hits: 0

[Summary of Liver Soul One Night: The most complete and the most detailed on the whole network] Teach you the installation and use of [PyQt5] step by step ☀️《❤️Remember to collect ❤️》

🏳️‍🌈Catalog

🏳️‍🌈 Lecture starts! ! ! ! 🏳️‍🌈

=====================================================================================================

⌛Foreword

Recently, I occasionally write some small tools to improve the efficiency of the project team. Since the intermediate modules that interact with physical devices are based on Python, it is difficult to communicate with them in Java. After a few GUI frameworks, I finally fell in love with PyQt5, a tool that is used more and has relatively complete documentation~

PyQt5 is based on the QT library, which is a C++ library and development tools including GUI, networking, threading, regular expressions, SQL databases, SVG, OpenGL, XML, user and application settings, positioning and positioning Services, short-range communication (NFC and Bluetooth), web browsing, 3D animations, charts, 3D data visualization and interface with app stores. PyQt5 implements over 1000 of these classes as a set of Python modules.

In short, PyQt5 is a GUI tool similar to Java Swing, which has built-in more advanced modules, and using PyQt Desinger, a tool such as PyQt Desinger can be used to graphically draw the interface and convert it into a py script file again.

Py Qt5 official website

⏳Install

The installation of Python3 is omitted here, and the pip tool is used to open CMD for installation:

pip install pyqt5

pip install pyqt5-tools

The first command is to install the core content of PyQt5, and the following pyqt5-tools is to install powerful supporting tools such as QtDesigner and PyUIC.

Of course, in order to finally package as an exe tool, you also need to install the pyinstaller module:

pip install pyinstaller

In fact, the dependency library has been installed above. Next, we configure the tools in PyCharm to facilitate quick opening.

⌚Configuration

There are mainly two tools configured here, one is a drawing tool, which directly draws the interface UI of the tool in HTML form, and the other is a tool that converts the HTML interface into a py file with one click.

Open Pycharm, click File–>Setting–>Tools–>External Tools
The right part was originally blank, click the “+” sign to set as follows:
Program: Fill in the PyQtDesignerinstallation path, that is, the path where the installation is designer.exelocated. The paths stored in different versions may be different. You have to go to the Python installation path to find them.

For example: the path after my installation is in D:\Python39\Lib\site-packages\qt5_applications\Qt\bin\designer.exe(see your own choice)

Just fill $FileDir$in the Working directory. It is more convenient to click Insert Macro… on the right and find it $FileDir$.

The second tool is PyUIC, which is a tool for converting the drawn interface UI into a py file, or File-->Setting-->Tools-->External Tools, after opening, add a tool:
Program: is the installation path of Python

Arguments: Fill in as follows:

-m PyQt5.uic.pyuic  $FileName$ -o $FileNameWithoutExtension$.py

Working directory:$FileDir$

So far, the basic settings have been completed, we can start a simple test.

⏰ Use

The drawing tools are automatically opened:
We choose the default, click OK to get an initial interface

in:

  • On the left is all the elements listed, including tables, input boxes, labels, checkboxes, drop-down boxes, etc.

  • In the middle is the drawing interface, you can move the elements yourself

  • On the right side is the object information of each element

  • In the middle of the right side are the properties of the selected element, which can be modified in size, default value, font, style, mouse hover strategy, etc.

  • Below the right side are the advanced properties, that is, the signal slot of PyQt5, which can complete multi-threaded operations

We draw a simple form as follows:
You can click Preview in the navigation bar above to preview the page:
Click Save as *.uifile , open it with PyCharm, and use the PyUIC tool to convert it to a py file:
To make the py file run, we need to add a main function as the startup entry, add the following code at the end of the py file just generated and introduce the corresponding dependencies:

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Ui_Dialog()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

At this time, we click to run the py file to open this interface!

🌡️Getting Started

Now we use the above form to do such a thing, click OK, the pop-up window prompts “Add success”, click “Reset” to clear all the content in the form, so we need to add dynamic operations in the py file, the whole logic comparison Simply, when you click the “OK” button, get the filled content of the above form, print it in the background, and add it successfully in the foreground pop-up window, and when you click “Reset”, clear all the content in the form.

self.pushButton.clicked.connect(lambda: self.submit_form())

Then add the details of the bound event:

def submit_form(self):
        input_name = self.lineEdit.text()
        input_username = self.lineEdit_2.text()
        input_password = self.lineEdit_3.text()
        input_sex = self.comboBox.currentText()
        input_remark = self.textEdit.toPlainText()
        print(input_name)
        print(input_username)
        print(input_password)
        print(input_sex)
        print(input_remark)
        reply = QMessageBox.information(MainWindow, 'success' , 'submit successful' , QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
         if reply == QMessageBox.Yes:
             print ( 'ok' )
         else :
             print ( 'cancel' )

Then the logic part of it is done! As for the reset function, you can try it yourself~

Here is all the code:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
import sys

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(463, 391)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(70, 40, 72, 15))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(70, 80, 72, 15))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(70, 120, 72, 15))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(70, 160, 72, 15))
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(Dialog)
        self.label_5.setGeometry(QtCore.QRect(70, 200, 72, 15))
        self.label_5.setObjectName("label_5")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(120, 80, 251, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
        self.lineEdit_2.setGeometry(QtCore.QRect(120, 120, 251, 21))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_3 = QtWidgets.QLineEdit(Dialog)
        self.lineEdit_3.setGeometry(QtCore.QRect(120, 40, 251, 21))
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.textEdit = QtWidgets.QTextEdit(Dialog)
        self.textEdit.setGeometry(QtCore.QRect(120, 190, 251, 87))
        self.textEdit.setObjectName("textEdit")
        self.comboBox = QtWidgets.QComboBox(Dialog)
        self.comboBox.setGeometry(QtCore.QRect(120, 160, 87, 22))
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(80, 320, 61, 28))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(lambda: self.submit_form())
        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(180, 320, 61, 28))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(Dialog)
        self.pushButton_3.setGeometry(QtCore.QRect(280, 320, 61, 28))
        self.pushButton_3.setObjectName("pushButton_3")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label_5.setText(_translate("Dialog", "Remarks: " self))

    def submit_form(self):
        input_name = self.lineEdit.text()
        input_username = self.lineEdit_2.text()
        input_password = self.lineEdit_3.text()
        input_sex = self.comboBox.currentText()
        input_remark = self.textEdit.toPlainText()
        print(input_name)
        print(input_username)
        print(input_password)
        print(input_sex)
        print(input_remark)
        reply = QMessageBox.information(MainWindow, 'success' , 'submission successful' , QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
         if reply == QMessageBox.Yes:
            print('ok')
        else:
            print('cancel')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Ui_Dialog()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

The results are as follows:

🏳️‍🌈 Follow Suzhou Program Dabai and keep updating technology sharing. Thank you for your support 🏳️‍🌈

You may also like...

Leave a Reply

Your email address will not be published.