GUI程序打包成exe--笔记

因项目需求,需要编写一个GUI图形界面控制;在网上查资料看到pyqt这个家伙,这个家伙是一个比较流行,强大的图形界面库,包含了多种类和方法十分好用。遗憾的事中文文档很稀少,但是也挡不住我们学习的热情。初次编写GUI,欢迎拍砖。

1,在pyqt中使用designer进行界面设计,保存好后使用 pyuic4  -o  *.py   *.ui 生成py文件导入到主程序文件中,也可以不生成py文件使用 uic.loadUi('*.ui', self)在主程序中加载

2,GUI程序,我就不一一注视了

# -*- coding: utf-8 -*-

# windows.py

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
import os
import subprocess

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)
    
#图形界面类
class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(665, 656)
        self.gridLayout_6 = QtGui.QGridLayout(Dialog)
        self.gridLayout_6.setObjectName(_fromUtf8("gridLayout_6"))
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setMaximumSize(QtCore.QSize(48, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.spinBox = QtGui.QSpinBox(Dialog)
        self.spinBox.setEnabled(True)
        self.spinBox.setObjectName(_fromUtf8("spinBox"))
        self.gridLayout.addWidget(self.spinBox, 0, 1, 1, 1)
        self.label_4 = QtGui.QLabel(Dialog)
        self.label_4.setMaximumSize(QtCore.QSize(60, 16))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.gridLayout.addWidget(self.label_4, 0, 2, 1, 1)
        self.spinBox_2 = QtGui.QSpinBox(Dialog)
        self.spinBox_2.setObjectName(_fromUtf8("spinBox_2"))
        self.gridLayout.addWidget(self.spinBox_2, 0, 3, 1, 1)
        self.label_6 = QtGui.QLabel(Dialog)
        self.label_6.setMaximumSize(QtCore.QSize(41, 16))
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.gridLayout.addWidget(self.label_6, 0, 4, 1, 1)
        self.spinBox_3 = QtGui.QSpinBox(Dialog)
        self.spinBox_3.setObjectName(_fromUtf8("spinBox_3"))
        self.gridLayout.addWidget(self.spinBox_3, 0, 5, 1, 1)
        self.gridLayout_6.addLayout(self.gridLayout, 0, 0, 1, 1)
        self.gridLayout_4 = QtGui.QGridLayout()
        self.gridLayout_4.setObjectName(_fromUtf8("gridLayout_4"))
        self.pushButton_6 = QtGui.QPushButton(Dialog)
        self.pushButton_6.setMaximumSize(QtCore.QSize(75, 23))
        self.pushButton_6.setObjectName(_fromUtf8("pushButton_6"))
        self.gridLayout_4.addWidget(self.pushButton_6, 0, 2, 1, 1)
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setMaximumSize(QtCore.QSize(47, 16))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.gridLayout_4.addWidget(self.label_2, 0, 0, 1, 1)
        self.pushButton_7 = QtGui.QPushButton(Dialog)
        self.pushButton_7.setMaximumSize(QtCore.QSize(75, 23))
        self.pushButton_7.setObjectName(_fromUtf8("pushButton_7"))
        self.gridLayout_4.addWidget(self.pushButton_7, 0, 5, 1, 1)
        self.comboBox = QtGui.QComboBox(Dialog)
        self.comboBox.setEditable(True)
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.gridLayout_4.addWidget(self.comboBox, 0, 1, 1, 1)
        self.comboBox_4 = QtGui.QComboBox(Dialog)
        self.comboBox_4.setEditable(True)
        self.comboBox_4.setObjectName(_fromUtf8("comboBox_4"))
        self.gridLayout_4.addWidget(self.comboBox_4, 0, 4, 1, 1)
        self.label_3 = QtGui.QLabel(Dialog)
        self.label_3.setMaximumSize(QtCore.QSize(48, 16))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.gridLayout_4.addWidget(self.label_3, 0, 3, 1, 1)
        self.gridLayout_6.addLayout(self.gridLayout_4, 1, 0, 1, 1)
        self.gridLayout_2 = QtGui.QGridLayout()
        self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
        self.label_5 = QtGui.QLabel(Dialog)
        self.label_5.setMaximumSize(QtCore.QSize(48, 16))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.gridLayout_2.addWidget(self.label_5, 0, 0, 1, 1)
        self.comboBox_3 = QtGui.QComboBox(Dialog)
        self.comboBox_3.setMinimumSize(QtCore.QSize(205, 20))
        self.comboBox_3.setEditable(True)
        self.comboBox_3.setObjectName(_fromUtf8("comboBox_3"))
        self.gridLayout_2.addWidget(self.comboBox_3, 0, 1, 1, 1)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setMaximumSize(QtCore.QSize(75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.gridLayout_2.addWidget(self.pushButton, 0, 2, 1, 1)
        self.gridLayout_6.addLayout(self.gridLayout_2, 2, 0, 1, 1)
        self.gridLayout_3 = QtGui.QGridLayout()
        self.gridLayout_3.setObjectName(_fromUtf8("gridLayout_3"))
        self.pushButton_2 = QtGui.QPushButton(Dialog)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.gridLayout_3.addWidget(self.pushButton_2, 7, 1, 1, 1)
        self.pushButton_4 = QtGui.QPushButton(Dialog)
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        self.gridLayout_3.addWidget(self.pushButton_4, 9, 1, 1, 1)
        self.pushButton_3 = QtGui.QPushButton(Dialog)
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.gridLayout_3.addWidget(self.pushButton_3, 8, 1, 1, 1)
        self.listWidget = QtGui.QListWidget(Dialog)
        self.listWidget.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
        self.listWidget.setObjectName(_fromUtf8("listWidget"))
        self.gridLayout_3.addWidget(self.listWidget, 0, 0, 10, 1)
        self.gridLayout_6.addLayout(self.gridLayout_3, 3, 0, 1, 1)
        self.gridLayout_5 = QtGui.QGridLayout()
        self.gridLayout_5.setObjectName(_fromUtf8("gridLayout_5"))
        self.pushButton_8 = QtGui.QPushButton(Dialog)
        self.pushButton_8.setObjectName(_fromUtf8("pushButton_8"))
        self.gridLayout_5.addWidget(self.pushButton_8, 0, 0, 1, 1)
        self.pushButton_5 = QtGui.QPushButton(Dialog)
        self.pushButton_5.setMaximumSize(QtCore.QSize(16777215, 23))
        self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
        self.gridLayout_5.addWidget(self.pushButton_5, 0, 1, 1, 1)
        self.progressBar = QtGui.QProgressBar(Dialog)
        self.progressBar.setMaximumSize(QtCore.QSize(16777215, 20))
        self.progressBar.setProperty("value", 0)
        self.progressBar.setObjectName(_fromUtf8("progressBar"))
        self.gridLayout_5.addWidget(self.progressBar, 1, 0, 1, 2)
        self.textEdit = QtGui.QTextEdit(Dialog)
        self.textEdit.setMaximumSize(QtCore.QSize(16777215, 101))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.gridLayout_5.addWidget(self.textEdit, 2, 0, 1, 2)
        self.gridLayout_6.addLayout(self.gridLayout_5, 4, 0, 1, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        Dialog.setTabOrder(self.spinBox, self.spinBox_2)
        Dialog.setTabOrder(self.spinBox_2, self.spinBox_3)
        Dialog.setTabOrder(self.spinBox_3, self.pushButton_6)
        Dialog.setTabOrder(self.pushButton_6, self.pushButton_7)
        Dialog.setTabOrder(self.pushButton_7, self.comboBox_3)
        Dialog.setTabOrder(self.comboBox_3, self.pushButton)
        Dialog.setTabOrder(self.pushButton, self.listWidget)
        Dialog.setTabOrder(self.listWidget, self.pushButton_2)
        Dialog.setTabOrder(self.pushButton_2, self.pushButton_3)
        Dialog.setTabOrder(self.pushButton_3, self.pushButton_4)
        Dialog.setTabOrder(self.pushButton_4, self.pushButton_5)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "词频统计", None))
        self.label.setText(_translate("Dialog", "单字频率", None))
        self.label_4.setText(_translate("Dialog", "关键字频率", None))
        self.label_6.setText(_translate("Dialog", "相似度", None))
        self.pushButton_6.setText(_translate("Dialog", "选择文件", None))
        self.label_2.setText(_translate("Dialog", "忽略字集", None))
        self.pushButton_7.setText(_translate("Dialog", "选择引擎", None))
        self.label_3.setText(_translate("Dialog", "分析引擎", None))
        self.label_5.setText(_translate("Dialog", "存储目录", None))
        self.pushButton.setText(_translate("Dialog", "选择目录", None))
        self.pushButton_2.setText(_translate("Dialog", "添加", None))
        self.pushButton_4.setText(_translate("Dialog", "清空", None))
        self.pushButton_3.setText(_translate("Dialog", "删除", None))
        self.pushButton_8.setText(_translate("Dialog", "停止", None))
        self.pushButton_5.setText(_translate("Dialog", "Run", None))




class mainExec(QtCore.QProcess):
     def __init__(self,cmd):
          super(mainExec,self).__init__()
          self.cmd = cmd
          
     def starts(self):
          num = len(self.cmd)
          for i,p in enumerate(self.cmd):
             self.execute(p)
             f = p.split("-f")[-1].replace("\\","/").split("/")[-1].decode("utf8")
             self.waitForFinished()
             print self.exitStatus
             if self.exitStatus == 0:
                    self.writeData("%s/%d"%(f,float('%0.2f'%((i+1)/float(num))) * 100))
             else:
                  self.writeData("%s %d"%(f,i))

#线程开启进程                 
class execCalcWords(QtCore.QThread):
    def __init__(self, cmds):
        super(execCalcWords, self).__init__()
        self.cmds = cmds
        self.pro =  QtCore.QProcess()
    def run(self):
        v = 100/len(self.cmds)
        for i,c in enumerate(self.cmds):
            self.pro.execute(c)
            self.pro.waitForFinished()
            f = c.split("-f")[-1].replace("\\","/").split("/")[-1].decode("utf8")
            print self.pro.exitStatus()
            if self.pro.exitStatus() == 0:
                self.emit(QtCore.SIGNAL("showMsg(QString)"),QtCore.QString(f).append(u' 执行成功!'))
            self.emit(QtCore.SIGNAL("value(int)"),(i+1)*v)
        self.emit(QtCore.SIGNAL("value(int)"),100) 
  

#控制类
class Window(QtGui.QDialog):
     def __init__(self,parent=None):
        super(Window,self).__init__(parent)
        self.ping = None
        self.eng = None
        self.cmd = None
        self.proc = None
        self.guiWindow = Ui_Dialog()
        self.guiWindow.setupUi(self)
        self.guiWindow.retranslateUi(self)
        self.guiWindow.comboBox_3.addItem(QtCore.QDir.currentPath())
        #self.guiWindow.comboBox.addItem(u"无")
        #guiWindow.pushButton()
        #guiWindow.comboBox_3.createButton("&Browse...", self.selectDir) 
        QtCore.QObject.connect(self.guiWindow.pushButton,QtCore.SIGNAL("clicked()"),self.selectDir)
        QtCore.QObject.connect(self.guiWindow.pushButton_2,QtCore.SIGNAL("clicked()"),self.addFile)
        QtCore.QObject.connect(self.guiWindow.pushButton_6,QtCore.SIGNAL("clicked()"),self.selectP)
        QtCore.QObject.connect(self.guiWindow.pushButton_7,QtCore.SIGNAL("clicked()"),self.selectEng)
        QtCore.QObject.connect(self.guiWindow.pushButton_3,QtCore.SIGNAL("clicked()"),self.delFile)
        QtCore.QObject.connect(self.guiWindow.pushButton_4,QtCore.SIGNAL("clicked()"),self.clear)
        QtCore.QObject.connect(self.guiWindow.pushButton_5,QtCore.SIGNAL("clicked()"),self.run)
        QtCore.QObject.connect(self.guiWindow.pushButton_8,QtCore.SIGNAL("clicked()"),self.stop)


     @QtCore.pyqtSlot()
     def selectDir(self):
        
        directory = QtGui.QFileDialog.getExistingDirectory(self, "Find Files",
                QtCore.QDir.currentPath())

        if directory:
            if  self.guiWindow.comboBox_3.findText(directory) == -1:
                 self.guiWindow.comboBox_3.addItem(directory)
            self.guiWindow.comboBox_3.setCurrentIndex(self.guiWindow.comboBox_3.findText(directory))
            
     @QtCore.pyqtSlot()
     def selectEng(self):
        fileName = QtGui.QFileDialog.getOpenFileName(self)
        if fileName:
            self.eng = fileName
            result = fileName.split("/")[-1].split(".")[0]
            self.guiWindow.comboBox_4.addItem(result)
            self.guiWindow.comboBox_4.setCurrentIndex(self.guiWindow.comboBox_4.findText(result))
     

     @QtCore.pyqtSlot()
     def selectP(self):
        fileName = QtGui.QFileDialog.getOpenFileName(self)
        if fileName:
            self.ping = fileName
            pin = fileName.split("/")[-1]
            self.guiWindow.comboBox.addItem(pin)
            self.guiWindow.comboBox.setCurrentIndex(self.guiWindow.comboBox.findText(pin))

            
     @QtCore.pyqtSlot()
     def addFile(self):
        #self.guiWindow.progressBar.setValue(0)
        fileName = QtGui.QFileDialog.getOpenFileNames(self)
        if fileName:
            self.guiWindow.listWidget.addItems(fileName)

     @QtCore.pyqtSlot()
     def delFile(self):
         count = self.guiWindow.listWidget.count()
         #print count
         if count > 0:
             if QtGui.QMessageBox.warning(self, u'确认', u'确定要删除?', QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel) == QtGui.QMessageBox.Ok:
                #print 'po'
                #self.guiWindow.listWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)#选择模式
                f = self.guiWindow.listWidget.selectedItems()
                for k in f:
                     self.guiWindow.listWidget.takeItem(self.guiWindow.listWidget.row(k))
                
        
     @QtCore.pyqtSlot()
     def clear(self):
         self.guiWindow.listWidget.clear()
     
     @QtCore.pyqtSlot()
     def run(self):
          
          count = self.guiWindow.listWidget.count()
          program = "python %s"%self.eng.toUtf8()
          argument = " %s -t %d -d %d -n %d -o %s"%(program,self.guiWindow.spinBox.value(),self.guiWindow.spinBox_3.value(),self.guiWindow.spinBox_2.value(),self.guiWindow.comboBox_3.currentText().toUtf8())
          if self.ping:
               argument = "%s -m %s"%(argument,self.ping.toUtf8())
          self.cmd =  [" %s -f  %s "%(argument,self.guiWindow.listWidget.item(i).text().toUtf8() ) for i in range(count)]
          '''
          v = 100
          num = len(self.cmd)
          for i,p in enumerate(self.cmd):
               f = p.split("-f")[-1].replace("\\","/").split("/")[-1].decode("utf8")
               self.proc.execute(p)
               self.proc.waitForFinished()
               if self.proc.exitStatus() == 0:
                  self.guiWindow.listWidget_2.addItem(u"%s 成功执行!"%f)
                  self.guiWindow.progressBar.setValue(float('%0.2f'%((i+1)/float(num))) * v)
               else:
                  self.guiWindow.listWidget_2.addItem(u"%s 执行失败!"%f)
                  self.guiWindow.progressBar.setValue(0)
          '''
          self.proc = execCalcWords(self.cmd)
          QtCore.QObject.connect(self.proc,
                               QtCore.SIGNAL("value(int)"),
                               self.guiWindow.progressBar.setValue)
          QtCore.QObject.connect(self.proc,
                               QtCore.SIGNAL("showMsg(QString)"),
                               self.guiWindow.textEdit.append)
          self.proc.start()   

     
          
                    
          
          
     @QtCore.pyqtSlot()
     def stop(self):
          #self.guiWindow.progressBar.setValue(0)
          #self.cmd = []
          #self.proc.environment()
          #self.proc.close()
          self.proc.terminate()

          #self.calcFinished()
         
    
    


            
        


if __name__=="__main__":
    app = QtGui.QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

3,打包程序

# -*- coding: utf-8 -*-
#py2exe_windows.py
from distutils.core import setup
import py2exe
import sys


sys.argv.append("py2exe")


py2exe_options = {
        "includes": ["sip"],
        "dll_excludes": ["MSVCP90.dll",],
        "compressed": 1,
        "optimize": 2,
        "ascii": 0,
        }


    
setup(
      name = 'PyQt Demo',
      version = '1.0',
      windows= ['C:/Users/irsadmin/Desktop/cwWin/myPro/windows.py',], 
      zipfile = None,
      options = {'py2exe': py2exe_options}
      )



4,打包命令

python  py2exe_windows.py  install

python py2exe_windows.py py2exe


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值