Table of contents
Set the shortcut startup method of the software
1. Programs for designing interfaces
2. Convert the ui file designed by Qt Designer into a py file
3. You can package the py file into an executable exe file
Convert the ui file of the designed ui interface into a py file
next chapter content
Configuration of PyQt5
There are tutorials on configuring other bloggers
It is recommended to install the following packages (it is best to enter the following command in your original computer environment, that is, the cmd of your computer, do not create it in the virtual environment created by pycharm, to prevent the software from being unable to open after the project is deleted)
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt5
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt5-tools
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple paramiko
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
Of course, it must be installed again in pycharm
after that in settings
Select Tools -> External Tools -> Plus Sign
Set the shortcut startup method of the software
Then create the following content in turn
1. Programs for designing interfaces
名称:Qt Designer
工具设置
程序:C:\Users\你的用户名\AppData\Local\Programs\Python\你的python版本\Lib\site-packages\qt5_applications\Qt\bin\designer.exe
工作目录:$FileDir$
2. Convert the ui file designed by Qt Designer into a py file
名称:PyUIC
工具设置:
程序:C:\Users\你的用户名\AppData\Local\Programs\Python\你的python版本\Scripts\pyuic5.exe
实参:$FileName$ -o $FileNameWithoutExtension$.py
工具目录:$FileDir$
3. You can package the py file into an executable exe file
名称:PyInstall
工具设置:
程序:C:\Users\你的用户名\AppData\Local\Programs\Python\你的python版本\Scripts\pyinstaller.exe
实参: -F -w $FileNameWithoutExtension$.py
工作目录:$FileDir$
4. Put the ico image in the qrc file, and then convert the qrc file into a py file for the icon of the gadget
名称:pyrcc
工具设置:
程序:C:\Users\你的名字\AppData\Local\Programs\Python\你的Python3版本\Scripts\pyrcc5.exe
实参:$FileName$ -o $FileNameWithoutExtension$.py
工作目录:$FileDir$
After writing, click Apply
Shortcut method
Then you can click the left triangle
Tools->External tools for quick use
design interface
Then we click Qt Designer to start designing the interface
Then start creating a window
After entering, create a window according to your preferences
Advanced usage methods are not introduced
I have used
label: the text in the box
line edit: link for getting input
Push Button: used to set the trigger button, such as start crawling or close the window
Text Browser: used to output the results of the program (optional, for the user to see)
After clicking the control, the box on the right will show which control it is
It is recommended to rename one side of the name, otherwise it will be difficult to remember
After the design is complete, you can click Save, which is saved to the root directory of your python project by default.
Convert the ui file of the designed ui interface into a py file
Since we set the shortcut
We can easily right-click the ui file and execute the PyUIC tool
Then you can find the py file with the same name in the file directory of the project
Or you can execute the command
pyuic5 -o 原ui文件名称 输出的py文件名称
The compiled file looks like this
The code for the file is
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'win.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# 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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(679, 485)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Button_run = QtWidgets.QPushButton(self.centralwidget)
self.Button_run.setGeometry(QtCore.QRect(50, 240, 121, 41))
self.Button_run.setObjectName("Button_run")
self.Button_close = QtWidgets.QPushButton(self.centralwidget)
self.Button_close.setGeometry(QtCore.QRect(220, 240, 121, 41))
self.Button_close.setObjectName("Button_close")
self.label_link = QtWidgets.QLabel(self.centralwidget)
self.label_link.setGeometry(QtCore.QRect(60, 110, 71, 21))
self.label_link.setObjectName("label_link")
self.line_link = QtWidgets.QLineEdit(self.centralwidget)
self.line_link.setGeometry(QtCore.QRect(130, 110, 211, 21))
self.line_link.setObjectName("line_link")
self.text_result = QtWidgets.QTextEdit(self.centralwidget)
self.text_result.setGeometry(QtCore.QRect(370, 110, 291, 321))
self.text_result.setObjectName("text_result")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 679, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "爬虫"))
self.Button_run.setText(_translate("MainWindow", "开始爬取"))
self.Button_close.setText(_translate("MainWindow", "关闭"))
self.label_link.setText(_translate("MainWindow", "目录链接"))
code in main file
Create a file named main.py (used to store our main program)
import sys
# PyQt5中使用的基本控件都在PyQt5.QtWidgets模块中
from PyQt5.QtWidgets import QApplication, QMainWindow
# 导入designer工具生成的login模块
from win import Ui_MainWindow
import time
And win is the window file I just compiled. The file Ui_mainWindow is my class name
(Don't just copy it all at once, just follow the gourd to draw a scoop)
class MyMainForm(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
# 固定的,PyQt5程序都需要QApplication对象。sys.argv是命令行参数列表,确保程序可以双击运行
app = QApplication(sys.argv)
# 初始化
myWin = MyMainForm()
# 将窗口控件显示在屏幕上
myWin.show()
# 程序运行,sys.exit方法确保程序完整退出。
sys.exit(app.exec_())
Together, and then run, you can see a window appears
but our button doesn't do anything
Next we will use the function to bind the event of pressing the button
just in the function
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
It can be changed to this (meaning that pressing the Button_close button triggers the close function, this function does not need to be defined by itself, press the Button_run button to execute the F_run function, which we need to define ourselves)
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
self.Button_close.clicked.connect(self.close)
self.Button_run.clicked.connect(self.F_run)