from UI import Ui_mainWindow
import sys
import os
import subprocess
# from PyQt5.QtWidgets import QApplication, QMainWindow, QInputDialog
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
# # 發射信號
# class Stream(QObject):
# newText = pyqtSignal(str)
#
# def write(self, text):
# self.newText.emit(str(text))
# QApplication.processEvents()
class Thread(QThread):
newText = pyqtSignal(str)
def __init__(self, py_spec_path=None, ico_path=None, is_f_checked=None, is_w_checked=None, is_i_checked=None,
exe_save_path=None, parent=None):
super(Thread, self).__init__(parent)
self.py_spec_path = py_spec_path
self.ico_path = ico_path
self.is_f_checked = is_f_checked
self.is_w_checked = is_w_checked
self.is_i_checked = is_i_checked
self.exe_save_path = exe_save_path
def write(self, text):
self.newText.emit(str(text))
QApplication.processEvents()
def __del__(self):
self.wait()
# def handleStdOut(self):
# data = self.process.readAllStandardOutput().data()
# print(data.decode('utf-8'))
#
# def handleStdErr(self):
# data = self.process.readAllStandardError().data()
# # self.display.append(data.decode('utf-8'))
# print(data.decode('utf-8'))
def run(self):
try:
# 1.py路径
if self.py_spec_path == '':
print('打包失败,请选择待打包的py或spec文件')
elif self.py_spec_path.split('.')[-1] == 'spec': # 选中的是spec文件
# -F -w -i 均无效
f_checked_status = ''
w_checked_status = ''
i_checked_status = ''
if self.exe_save_path == '':
self.exe_save_path = os.getcwd()
# 打包spec文件时,不要specpath参数
exe_save_path_ = f'--distpath {self.exe_save_path} --workpath {self.exe_save_path}/build'
# 执行打包命令
print('开始打包,请稍候……')
cmd = f'pyinstaller {f_checked_status} {w_checked_status} {i_checked_status} {self.py_spec_path} {exe_save_path_}'
# print(cmd)
os.system(cmd)
# print(subprocess.Popen(cmd))
print(f'完成打包,可通过“查看exe”按钮快速查看')
else:
# 2.-F
if self.is_f_checked == True:
f_checked_status = '-F'
else:
f_checked_status = ''
# 3.-w
if self.is_w_checked == True:
w_checked_status = '-w'
else:
w_checked_status = ''
# 4.-i
if self.is_i_checked == True:
if self.ico_path == '':
print('未选中ico图标,打包时不使用ico图标')
i_checked_status = ''
else:
i_checked_status = f'-i {self.ico_path}'
else:
i_checked_status = ''
# 5.exe保存路径
# 如果勾选但没有选到路径,仍默认到当前路径
if self.exe_save_path == '':
self.exe_save_path = os.getcwd()
exe_save_path_ = f'--distpath {self.exe_save_path} --specpath {self.exe_save_path} --workpath {self.exe_save_path}/build'
# 执行打包命令
print('开始打包,请稍候……')
cmd = f'pyinstaller {f_checked_status} {w_checked_status} {i_checked_status} {self.py_spec_path} {exe_save_path_}'
# print(cmd)
os.system(cmd)
# print(subprocess.Popen(cmd))
print(f'完成打包,可通过“查看exe”按钮快速查看')
except Exception as e:
print(e)
class MainWindow(QMainWindow, Ui_mainWindow):
def __init__(self):
QMainWindow.__init__(self)
Ui_mainWindow.__init__(self)
self.setupUi(self)
# sys.stdout = Stream(newText=self.onUpdateText)
# sys.stderr = Stream(newText=self.onUpdateText)
self.th = Thread()
self.th.newText.connect(self.onUpdateText)
sys.stdout = self.th
sys.stderr = self.th
# def initUI(self):
# 选择py或spec文件
self.py_seclector_button.clicked.connect(self.seclector_py_spec)
# 选择ico文件
self.ico_seclector_button.clicked.connect(self.seclector_ico)
# 选择保存路径
self.seclector_custom_path_button.clicked.connect(self.seclector_custom_path)
# 打包按钮
self.pack_button.clicked.connect(self.pack)
# 查看exe
self.show_exe_button.clicked.connect(self.show_exe)
# 查看exe
def show_exe(self):
try:
exe_folder = self.seclector_custom_path_edit.text()
if exe_folder == '':
exe_folder = os.getcwd() # 当前路径
# 打開文件夾
os.startfile(exe_folder)
except Exception as e:
print(e)
# 选择py或spec文件
def seclector_py_spec(self):
try:
directory = QFileDialog.getOpenFileName(self, '选择py或spec文件', './', "all files(*.*)")
self.py_seclector_edit.setText(directory[0])
except:
pass
# 选择ico文件
def seclector_ico(self):
try:
# 如果选择了py文件,ico默认为py同路径
self.ico_path = self.py_seclector_edit.text().split('/')
self.ico_path = '/'.join(self.ico_path[:-1]) # 去除最后一项,重新拼回去
if self.ico_path == '':
self.ico_path = os.getcwd() # 如果py
directory = QFileDialog.getOpenFileName(self, '选择ico文件', f'{self.ico_path}', "all files(*.ico)")
self.ico_seclector_edit.setText(directory[0])
except:
pass
# 选择exe保存路径
def seclector_custom_path(self):
try:
directory = QFileDialog.getExistingDirectory(None, '选择打包exe保存路径', "./") # 起始路径
self.seclector_custom_path_edit.setText(directory)
except:
pass
# 执行打包
def pack(self):
self.show_word_edit.setText('')
self.thread = Thread()
self.thread.py_spec_path = self.py_seclector_edit.text()
self.thread.ico_path = self.ico_seclector_edit.text()
self.thread.is_f_checked = self.F_checkBox.isChecked()
self.thread.is_w_checked = self.w_checkBox.isChecked()
self.thread.is_i_checked = self.i_checkBox.isChecked()
# 如果“同路径保存”被选中,则路径为同路径
if self.seclect_this_path_radioButton.isChecked():
self.thread.exe_save_path = os.getcwd() # 当前路径
elif self.seclector_custom_path_radioButton.isChecked():
self.thread.exe_save_path = self.seclector_custom_path_edit.text()
self.thread.start()
def onUpdateText(self, text):
"""Write console output to text widget."""
cursor = self.show_word_edit.textCursor()
cursor.movePosition(QTextCursor.End)
cursor.insertText(text)
self.show_word_edit.setTextCursor(cursor)
self.show_word_edit.ensureCursorVisible()
def closeEvent(self, event):
reply = QMessageBox.question(self, '退出提示',
"您确定要退出吗?", QMessageBox.Yes |
没有合适的资源?快使用搜索试试~ 我知道了~
一款自用的pyinstaller打包辅助工具+源码.rar
共5个文件
py:2个
ui:1个
png:1个
需积分: 15 6 下载量 176 浏览量
2022-09-18
10:47:48
上传
评论 1
收藏 33.94MB RAR 举报
温馨提示
今天分享一款自用的pyinstaller打包辅助工具,把pyinstaller打包的常用的选项内置在工具中,勾选后点击打包即可。注:-F -w -i 参数仅对打包.py文件时有效,如果是打包spec文件时,这三个参数勾选与否都无关保存路径的话可默认与打包工具同路径,或自定义保存路径。(build文件夹、spec文件、exe文件三者均会在目标路径下生成) 不足之处:1.程序是调用os.system(cmd)执行打包,但还不会把控制台的代码在工具中显示,所以打包过程中,会有窗口弹出,打包完成后,自动关闭。2.win10打包的工具,win7可能用不了。需要的朋友可以自行编译。
资源详情
资源评论
资源推荐
收起资源包目录
一款自用的pyinstaller打包辅助工具+源码.rar (5个子文件)
一款自用的pyinstaller打包辅助工具+源码
源码
MainWindow.py 8KB
UI.py 8KB
UI.ui 6KB
Pyinstaller打包辅助工具.png 11KB
pyinstaller打包辅助工具.exe 34.18MB
共 5 条
- 1
大飞哥软件自习室
- 粉丝: 575
- 资源: 1343
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0