600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 使用Pyqt5制作GUI界面 并使用pyinstaller打包成exe文件

使用Pyqt5制作GUI界面 并使用pyinstaller打包成exe文件

时间:2022-11-29 13:35:38

相关推荐

使用Pyqt5制作GUI界面 并使用pyinstaller打包成exe文件

1:安装需要的第三方库(pyqt5,pyqt5-tools)

如果读者使用的anaconda,在安装完上述两个包之后会出现spyder打开不了的情况,本博客是基于纯python3(重装 的,自己的spyder也打不开了,还没整好)制作的GUI以及打包exe。

2:首先使用qt-designer,制作GUI界。

2.1新建 main window界面。在D:\py3\Lib\site-packages\PyQt5\Qt\bin下打开qt designer

制作以下界面(主要功能包括打开图片,保存,放大,缩小)

保存test.ui文件,在CMD中利用

转换成py文件

3:编写数据处理主要代码(data_process.py)

import gdalimport osimport numpyimport matplotlib.pyplot as pltfrom PIL import Image,ImageGrabimport matplotlibmatplotlib.rcParams['font.family']='SimHei'matplotlib.rcParams['font.sans-serif']='SimHei'class imageedit_process:def read_img(path):img= Image.open(path)#plt.subplot(111),plt.imshow(img,'gray'),plt.title('origin_img')#print(img)#plt.show()return imgdef zoomin(path):#放大img = Image.open(path) # 读取的图像显示的<matplotlib.image.AxesImage object at 0x7f9f0c60f7f0>region = img.transpose(Image.ROTATE_180) #翻转(x, y) = img.sizeout = img.resize((x+1000, y+1000)) # 改变大小plt.subplot(121),plt.imshow(img,'gray'),plt.title('原始影像')plt.subplot(122),plt.imshow(out,'gray'),plt.title('放大影像')plt.show()def zoomout(path):#缩小img = Image.open(path) # 读取的图像显示的<matplotlib.image.AxesImage object at 0x7f9f0c60f7f0>(x, y) = img.sizeout = img.resize((x-200, y-200)) # 改变大小plt.subplot(121),plt.imshow(img,'gray'),plt.title('原始图像')plt.subplot(122),plt.imshow(out,'gray'),plt.title('缩小影像')plt.show()

4,运行代码(run_demo.py)

from PyQt5 import QtWidgetsfrom test2 import Ui_MainWindow#from PyQt5.QtWidgets import QFileDialogfrom data_process import imageedit_process#from bottombutton import button_selffrom PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *class mywindow(QtWidgets.QMainWindow, Ui_MainWindow):def __init__ (self):super(mywindow, self).__init__()self.setupUi(self)self.actionopen.triggered.connect(self.read_file)#打开self.actionsave.triggered.connect(self.save_file)#保存self.actionzoom_in.triggered.connect(self.zoom_in)self.actionzoom_out.triggered.connect(self.zoom_out)def read_file(self):#选取文件filename, filetype =QFileDialog.getOpenFileName(self, "open file", "D:/imagetest", "All Files(*);;Text Files(*.png)")print(filename, filetype)self.lineEdit.setText(filename)self.pic_show.setPixmap(QPixmap(filename))#img=self.read_img(filename)def save_file(self):#获取文件路径# 用全局变量保存所有需要保存的变量在内存中的值。file_name = QFileDialog.getSaveFileName(self,"文件保存","D:/imagetest/save","All Files (*);;Text Files (*.png)")print(file_name[0])btn=button_self()btn.file_save(file_path,file_name[0])#imageedit_process.read_img(filename)def zoom_in(self):file_path = self.lineEdit.text()if file_path=='':self.showMessageBox()else:imageedit_process.zoomin(file_path)def zoom_out(self):file_path = self.lineEdit.text()if file_path=='':self.showMessageBox()else:imageedit_process.zoomout(file_path)def showMessageBox(self):res_3 = QMessageBox.warning(self, "警告", "请选择文件,再执行该操作!", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)if __name__=="__main__":import sysapp=QtWidgets.QApplication(sys.argv)ui = mywindow() ui.show()sys.exit(app.exec_())

效果图如下:

5;使用pyinstaller打包成exe文件

#!/usr/bin/env python3# -\- coding: utf-8 -\-from PyInstaller.__main__ import runimport os# -F:打包成一个EXE文件 # -w:不带console输出控制台,window窗体格式 # --paths:依赖包路径 # --icon:图标 # --noupx:不用upx压缩 # --clean:清理掉临时文件if __name__ == '__main__':opts = ['-F', '-w', '--paths=D:\py3\Lib\site-packages\PyQt5\Qt\bin','--paths=D:\py3\Lib\site-packages\PyQt5\Qt\plugins','--noupx', '--clean',r'F:\s2_download\qt5manager-master\test2\run_demo.py']run(opts)

其中run_demo.py是运行的文件新生成一个dist文件夹,生成一个exe文件。

详细代码及界面设置详见/download/qq_33657870/12324201**(不要积分)**

test2.ui为qt designer中设计的界面

test2.py为ui转成的py文件

data_process为数据处理的代码(现在和界面无关)

run_demo为运行的代码将数据处理功能和界面关联

py2exe为代码打包成exe文件

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。