Quick Development of GUI Programs Using PyQt5 in Windows Environment--Configuration of Pycharm

Posted by jlh3590 on Thu, 09 Jan 2020 19:36:37 +0100

Environment Configuration
Install PyQt5
Installation using pip makes it easier to use Pycharm and search directly.
In addition to pyqt5, there are pyqt5-tools, which contain some commonly used tools for pyqt5.

pip install pyqt5 -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
pip install pyqt5-tools -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

Set Qt Designer

After installing pyqt5-tools, you can set up Qt Designer so that we can quickly draw the interface by dragging and dropping.
Add Qt Designer to Settings-Tools-External Tools.
Select the path to designer.exe in the Program, complete with D:\Python\Python37\Lib\site-packages\pyqt5_toolsQtbin\designer.exe
Working directory selects Insert Macros and File - directory
Example:

Now that Qt Designer has been added, we can see the tools we just added in Tools-External Tools, where other added tools can also be used.
Set PyUIC

The interface we just designed with Qt Designer is actually an xml file with the suffix ui. Python code is not directly usable yet. You need to use PyUIC to convert the UI file to py code.
PyUIC settings are similar to Qt Designer in that they mainly change Program to a PyUIC path, the full path being D:\Python\Python37\python.exe.
The difference here is that you need to write parameters to PyUIC in Parameters, so here we add the parameter - M PyQt5.uic.py UIC $FileName$-o $FileNameWithoutExtension$.py.
Working directory selects Insert Macros and File - directory
Example:

Set up Pyrcc
If a qrc resource file is used in Qt Designer without converting it to a py file, an error will occur: import error no module named resource_rc
This is because after using the qrc resource file, when converting ui file to py file with pyuic, import resource_rc. Without this file, all related qrc files need to be converted first and placed in the same folder as the pyuic file generated by pyUIC.
This replaces the Program with a Pyrcc path, the full path being D:\Python\Python37

shi\Scripts\pyrcc5.exe.
Write the parameters passed to Pyrcc in Parameters, and here add the parameter $FileName$-o $FileNameWithoutAllExtensions$_rc.py
Working directory selects Insert Macros and File - directory

Use:

Run qt designer under working directory (C:Users15305PycharmProjectspy3PyDataDemo>)

Generate pyqt5btn1.ui()

Right-click pyqt5btn1.ui PyUIC tool will generate pyqt5btn1.py

 

Add the following code to the end of pyqt5btn1.py

if  __name__=="__main__":
    app = QtWidgets.QApplication(sys.argv)
    main = QtWidgets.QDialog()              #Main dialog QDialog must be created
    content = Ui_Dialog()                   #create a dialog box
    content.setupUi(main)                   #Attach dialog box to main form
    main.show()                             #display
    sys.exit(app.exec_())

Complete code: (pyqt5btn1.py)

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

# Form implementation generated from reading ui file 'pyqt5btn1.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
import sys

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")

        self.retranslateUi(Dialog)
        self.buttonBox.accepted.connect(Dialog.accept)
        self.buttonBox.rejected.connect(Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

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


if  __name__=="__main__":
    app = QtWidgets.QApplication(sys.argv)
    main = QtWidgets.QDialog()              #Main dialog QDialog must be created
    content = Ui_Dialog()                   #create a dialog box
    content.setupUi(main)                   #Attach dialog box to main form
    main.show()                             #display
    sys.exit(app.exec_())

Run pyqt5btn1.py

Reference resources:

Pthon, pycharm, pyqt5 installation and configuration complete set of detailed processes

Various pits for PyQt5 installation

Building Python3+PyQt5+PyCharm Desktop GUI Development Environment

Topics: Programming Qt Python pip Pycharm