PyQty5—Lesson 1: Installation and simple interface design (with code)

In our daily office work, we will cooperate with PyQty5 to design a small GUI program, which looks good without the interface and can be packaged and sent to our relatives and friends for use, so today we will carry out PyQty5 first class of

1. First we need to install the PyQty5 library

library name Install
sip pip install sip
PyQt5 pip install PyQt == 5 5.12.1
pyqt5-tools pip install pyqt5-tools == 5.11.2.1.3

2. After all are installed, we configure the extension tool on pycharm ( 一共要配置两个)

Configure the first one:用来设计GUI

2-1, first of all, we find the path of the * * just installed pyqt5-tools, you can look for it with my path

insert image description here

2-3, copy the path of [designer.exe] for later use

insert image description here

2-4, we click on the settings of Pycharm - click Tools - click External Tools - click [ + ] plus sign

insert image description here
insert image description here
insert image description here

3. Configure the second

把设计好的GUI变成python代码文件

The first box sets the name (write whatever you want)

The second box sets the python interpreter path (note: 我这没用Anaconda虚拟环境,这里小伙伴根据自己的环境进行选择)

The third box: fill in as follows

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

The fourth box: fill in as follows

$FileDir$

insert image description here

4. Check whether the configuration is successful:

4-1 Create a new py file casually, right click as shown in the figure below and the configuration is successful! !

insert image description here

4-2 Click [QT] in the picture above (my name is QT), and it will appear after a while

insert image description here

All of the above are successful, indicating that our configuration is successful

insert image description here

Design it casually by yourself, I just learned it, and I drew it blindly, the initial interface introduction: please click me

5. Convert ui file to py file

insert image description here

6. Add at the end:

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

full code

# -*- coding: utf-8 -*-
import sys

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# 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.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(993, 563)
        self.tabWidget = QtWidgets.QTabWidget(Dialog)
        self.tabWidget.setGeometry(QtCore.QRect(10, 0, 621, 41))
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.tabWidget.addTab(self.tab, "")
        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.tabWidget.addTab(self.tab_2, "")
        self.horizontalLayoutWidget = QtWidgets.QWidget(Dialog)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 50, 621, 80))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.comboBox = QtWidgets.QComboBox(self.horizontalLayoutWidget)
        self.comboBox.setObjectName("comboBox")
        self.horizontalLayout.addWidget(self.comboBox)
        self.textBrowser = QtWidgets.QTextBrowser(Dialog)
        self.textBrowser.setGeometry(QtCore.QRect(10, 140, 256, 192))
        self.textBrowser.setObjectName("textBrowser")

        self.retranslateUi(Dialog)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Dialog", "Tab 1"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Dialog", "Tab 2"))
        self.label.setText(_translate("Dialog", "选择文件夹"))
        self.textBrowser.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">文件有:</p></body></html>"))

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

This is our first lesson in learning PyQty5 today, and I will study it carefully next time

In the next lesson, we will learn to design an interface, then customize some button functions, and gradually understand how to use some controls

I hope everyone has to help

A little programmer dedicated to office automation#

I've seen this, follow + like + bookmark = don't get lost! !

If you want to know more about Python office automation, please pay attention!

Guess you like

Origin blog.csdn.net/weixin_42636075/article/details/132408399