1、安装软件和模块pycharm+pyqt5+pyqt5-tool+pyinstaller,以及相关配置
2、在QT中创建界面,生成ui文件
注:使用帮助文档,按一次“F1”即可全屏显示帮助文档页面,按“Esc”即可退出。
3、通过pyuic5 -o Interface.py Interface.ui,将ui文件转为py文件。
4、调用,新建RecallInterface.py,以及自己主函数f1.py
import sys
#PyQt5中使用的基本控件都在PyQt5.QtWidgets模块中
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import threading
#导入designer工具生成的Interface.py
from XXA import Ui_MainWindow
#导入自己设计的信息函数
from f1 import NewoneEVE
class RecallInterface (QMainWindow, Ui_MainWindow):
#初始化,定义添加信息槽
def __init__(self, parent=None):
super(RecallInterface, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#初始化界面信息
self.init_login_info()
#触发事件
self.ui.OK.clicked.connect(self.begin)
self.ui.Reset.clicked.connect(self.clearResult)
self.ui.openfile.clicked.connect(self.msge)
self.url = "https://www.baidu.com/index.php?tn=monline_3_dg"
self.ui.pushButton.clicked.connect(self.msg)
# 保存登录信息
def save_login_info(self):
settings = QSettings("config.ini", QSettings.IniFormat) # 方法1:使用配置文件
# settings = QSettings("mysoft","myapp") #方法2:使用注册表
settings.setValue("excelpath", self.ui.excelpath.text())
settings.setValue("readsheetname", self.ui.readsheetname.text())
settings.setValue("browser", self.ui.browser.currentText())
settings.setValue("driver_path", self.ui.driver_path.text())
settings.setValue("startnum", self.ui.startnum.text())
settings.setValue("rownum", self.ui.row.text())
settings.setValue("endnum", self.ui.endnum.text())
settings.setValue("sleeptime", self.ui.sleeptime.text())
settings.setValue("topage", self.ui.topage.text())
settings.setValue("toline", self.ui.toline.text())
settings.setValue("need_domain", self.ui.need_domain.currentText())
#初始化界面信息
def init_login_info(self):
settings = QSettings("config.ini", QSettings.IniFormat) #方法1:使用配置文件
#settings = QSettings("mysoft","myapp") #方法2:使用注册表
excelpath =settings.value("excelpath")
readsheetname = settings.value("readsheetname")
browser = settings.value("browser")
driver_path = settings.value("driver_path")
startnum = settings.value("startnum")
endnum= settings.value("endnum")
rownum = settings.value("rownum")
sleeptime = settings.value("sleeptime")
topage = settings.value("topage")
toline = settings.value("toline")
need_domain = settings.value("need_domain")
########
self.ui.excelpath.setText(excelpath)
self.ui.readsheetname.setText(readsheetname)
self.ui.browser.setCurrentText(browser)
self.ui.driver_path.setText(driver_path)
self.ui.startnum.setText(startnum)
self.ui.endnum.setText(endnum)
self.ui.row.setText(rownum)
self.ui.sleeptime.setText(sleeptime)
self.ui.topage.setText(topage)
self.ui.toline.setText(toline)
self.ui.need_domain.setCurrentText(need_domain)
#打开文件
def msge(self):
# directory1 = QFileDialog.getExistingDirectory(self,"选取文件夹","./") #起始路径
fileName1, filetype1 = QFileDialog.getOpenFileName(self, "选取文件", "./",
"All Files (*)") # 设置文件扩展名过滤,注意用双分号间隔
self.ui.excelpath.setText(fileName1)
def msg(self):
fileName2, filetype2 = QFileDialog.getOpenFileName(self, "选取文件", "./",
"All Files (*)") # 设置文件扩展名过滤,注意用双分号间隔
self.ui.driver_path.setText(fileName2)
def clearResult(self):
self.ui.excelpath.clear()
self.ui.readsheetname.clear()
self.ui.driver_path.clear()
self.ui.startnum.clear()
self.ui.endnum.clear()
self.ui.sleeptime.clear()
self.ui.topage.clear()
self.ui.toline.clear()
self.ui.output.clear()
def begin(self):
#保存配置
self.save_login_info()
startnum = int(self.ui.startnum.text())
endnum = int(self.ui.endnum.text())
rownum = int(self.ui.row.text())
excelpath = self.ui.excelpath.text()
readsheetname = self.ui.readsheetname.text()
sleeptime = int(self.ui.sleeptime.text())
browser = self.ui.browser.currentText()
driver_path = self.ui.driver_path.text()
topage = int(self.ui.topage.text())
toline = int(self.ui.toline.text())
need_domain=self.ui.need_domain.currentText()
# 第3/3
#调用主程序,New0111.py文件中NewoneEVE函数,并将返回值显示在界面上
ndomin,npage,nline,mainbody=NewoneEVE(startnum, endnum, rownum, excelpath, readsheetname, sleeptime, browser,driver_path, topage, toline, need_domain)
self.ui.output.setText('当前信息:'+str(ndomin)+" "+str(mainbody)+" "+'当前页【'+ str(npage)+ '】;当前行【'+ str(nline)+ '】')
if __name__ == "__main__":
#固定的,PyQt5程序都需要QApplication对象。sys.argv是命令行参数列表,确保程序可以双击运行
app = QApplication(sys.argv)
#初始化
myWin = RecallInterface()
#将窗口控件显示在屏幕上
myWin.show()
#程序运行,sys.exit方法确保程序完整退出。
sys.exit(app.exec_())
5、为实验代码的可迁移性,使用命令行将文件打包生成exe文件。命令成功运行后,将会在项目目录中看到build文件夹和dist文件夹,把dist文件夹中的文件打包给第三方使用即可。
(1) 需要了解pyinstaller相关参数以及使用的格式pyinstaller文档
pyinstaller -F -w RecallInterface.py
pyinstaller -F -w RecallInterface.py -i Documents.ico
pyinstaller -D RecallInterface.py Interface.py f1.py
常见的用法
-F:dist文件下只有exe文件,把所有东西打包成一个exe文件,结构简单,启动慢。
-D(默认):dist文件下有exe文件以及依赖的附件,打包成多个文件,结构复杂,启动快。
-w:没有命令行窗口
-i:exe文件的图标
(2)多个文件打包
- 命令行打包
#RecallInterface.py,Interface.py, f1.py在同一目录下,默认是以-D形式打包
pyinstaller RecallInterface.py Interface.py, f1.py
pyinstaller RecallInterface.py -p C:/Interface.py, -p C:/f1.py
注:到切换到主文件路径下执行命令,不然要写RecallInterface.py,Interface.py, f1.py的完整路径。
- pyi-makespec打包
pyi-makespec RecallInterface.py
修改.spec文件spec文档
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['RecallInterface.py', 'Interface.py', 'f1.py'],
pathex=['C:\\pythonProject\\crawer'],
binaries=[],
datas=[],
#此处可以添加静态资源,例如你有个图片文件夹imgs,可以这样写[('imgs','imgs'),('test.txt','.')],打包以后会有一个一样的文件夹,点表示当前文件夹。
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='RecallInterface',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,# 打包的时候进行压缩,False表示不压缩
upx_exclude=[],# 是否显示黑窗口,刚开始打包的时候一般都会有问题,建议设为True,解决所有问题后可以设置为False)
runtime_tmpdir=None,
console=True )
执行命令
pyinstaller RecallInterface.spec