工作中需要開發一個小工具,簡單的UI界面可以很好的提高工具的實用性,由此開啟了我的第一次GUI開發之旅,下面將自己學習的心得記錄一下,也做為學習筆記吧!!!
參考:http://www.qaulau.com/books/PyQt4_Tutorial/index.html
一,Python GUI開發之PYQT4
1,首先安裝PYQT4
可以在 http://qunying.jb51.net:81/201704/tools/PyQt4_py2.7_x64_jb51.rar 這里,獲得PyQt4的下載,請注意選擇正確的Python版本和Python的位數。
下載解壓之后,雙擊安裝文件,下一步安裝即可,選擇好Python27的路徑。
在 :C:\Python27\Lib\site-packages\PyQt4 (自動轉換成你自己的路徑)下面找到 ,這個程序即GUI開發的工具界面。(打開它你就可以拖拖拽拽實現GUI的開發了),如下圖:
選擇好窗口類型,點擊“創建”就可以生成空白的窗口界面,你就可以在上面開發你自己的UI界面了。
2,將.ui 文件轉換成.py的代碼文件:
編輯好ui界面后,點擊保存,會生成一個.ui格式的文件。
在CMD下執行如下命令:pyuic4 xxx.ui -o xxx.py 這樣就會生成相應的Python代碼文件。
3. 從你的IDE中打開Python代碼,對其進行功能實現即可.在這里貼出我的代碼,僅供參考學習
首先,我做的是一個實現usb自動通斷的工具;界面是這樣的
然后,代碼如下:
# -*- coding: utf-8 -*- import os,sys from PyQt4 import QtCore, QtGui import ctypes import time import threading 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(397, 244) self.groupBox_3 = QtGui.QGroupBox(Dialog) self.groupBox_3.setGeometry(QtCore.QRect(10, 20, 381, 211)) self.groupBox_3.setFocusPolicy(QtCore.Qt.WheelFocus) self.groupBox_3.setAcceptDrops(False) self.groupBox_3.setAutoFillBackground(True) self.groupBox_3.setInputMethodHints(QtCore.Qt.ImhNone) self.groupBox_3.setObjectName(_fromUtf8("groupBox_3")) self.label_5 = QtGui.QLabel(self.groupBox_3) self.label_5.setGeometry(QtCore.QRect(10, 30, 51, 21)) self.label_5.setLineWidth(1) self.label_5.setMidLineWidth(1) self.label_5.setTextFormat(QtCore.Qt.AutoText) self.label_5.setAlignment(QtCore.Qt.AlignCenter) self.label_5.setWordWrap(True) self.label_5.setMargin(0) self.label_5.setObjectName(_fromUtf8("label_5")) self.label_6 = QtGui.QLabel(self.groupBox_3) self.label_6.setGeometry(QtCore.QRect(10, 70, 51, 21)) self.label_6.setLineWidth(1) self.label_6.setMidLineWidth(1) self.label_6.setTextFormat(QtCore.Qt.AutoText) self.label_6.setAlignment(QtCore.Qt.AlignCenter) self.label_6.setWordWrap(True) self.label_6.setMargin(0) self.label_6.setObjectName(_fromUtf8("label_6")) self.spinBox = QtGui.QSpinBox(self.groupBox_3) self.spinBox.setGeometry(QtCore.QRect(80, 70, 70, 22)) self.spinBox.setMaximum(10000) self.spinBox.setValue(60) self.spinBox.setObjectName(_fromUtf8("spinBox")) self.label_7 = QtGui.QLabel(self.groupBox_3) self.label_7.setGeometry(QtCore.QRect(10, 110, 51, 21)) self.label_7.setLineWidth(1) self.label_7.setMidLineWidth(1) self.label_7.setTextFormat(QtCore.Qt.AutoText) self.label_7.setAlignment(QtCore.Qt.AlignCenter) self.label_7.setWordWrap(True) self.label_7.setMargin(0) self.label_7.setObjectName(_fromUtf8("label_7")) self.spinBox_2 = QtGui.QSpinBox(self.groupBox_3) self.spinBox_2.setGeometry(QtCore.QRect(80, 110, 70, 22)) self.spinBox_2.setMaximum(10000) self.spinBox_2.setValue(2) self.spinBox_2.setObjectName(_fromUtf8("spinBox_2")) self.radioButton = QtGui.QRadioButton(self.groupBox_3) self.radioButton.setGeometry(QtCore.QRect(80, 30, 89, 21)) self.radioButton.setAutoRepeat(False) self.radioButton.setObjectName(_fromUtf8("radioButton")) self.pushButton = QtGui.QPushButton(self.groupBox_3) self.pushButton.setGeometry(QtCore.QRect(280, 110, 70, 21)) self.pushButton.setObjectName(_fromUtf8("pushButton")) self.progressBar = QtGui.QProgressBar(self.groupBox_3) self.progressBar.setGeometry(QtCore.QRect(10, 170, 361, 23)) self.progressBar.setProperty("value", 0) self.progressBar.setObjectName(_fromUtf8("progressBar")) #以上代碼都是自動生成的,沒有什么難度
#下面的代碼是主要是對控件功能的實現
self.radioButton.toggled.connect(self.changeUSBstatus) self.pushButton.clicked.connect(lambda:self.usbSwitchThreads()) # self.pushButton.connect(self.pushButton, SIGNAL("clicked"),self.changeUSBstatus()) # QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.pushButton.toggle) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): Dialog.setWindowTitle(_translate("Dialog", "Dialog", None)) self.groupBox_3.setTitle(_translate("Dialog", "USB通斷控制", None)) self.label_5.setText(_translate("Dialog", "初始狀態", None)) self.label_6.setText(_translate("Dialog", "間隔(s)", None)) self.label_7.setText(_translate("Dialog", "切換次數", None)) self.radioButton.setText(_translate("Dialog", "接通/斷開", None)) self.pushButton.setText(_translate("Dialog", "點擊執行", None)) def changeUSBstatus(self): if self.radioButton.isChecked(): USBcontrol().connectUsb() else: USBcontrol().disconnectUsb() def stopUsbSwitch(self): self.spinBox_2.setValue(0) def excuteUsbSwitch(self): self.pTime=self.spinBox.value() self.eTimes=self.spinBox_2.value() self.progressBar.setMinimum(0) self.progressBar.setMaximum(self.eTimes) for i in range(self.eTimes): time.sleep(int(self.pTime)) USBcontrol().connectUsb() time.sleep(int(self.pTime)) USBcontrol().disconnectUsb() self.progressBar.setValue(i+1) def usbSwitchThreads(self): threadsList=[] t = threading.Thread(target=self.excuteUsbSwitch,args=() ) threadsList.append(t) for t in threadsList: t.setDaemon(True) t.start() class USBcontrol: def __init__(self): resDict={ 0:"成功", 1:""} self.resDict = resDict dllPath = os.path.abspath(os.path.dirname(__file__)) self.objdll = ctypes.windll.LoadLibrary(dllPath+r'\usbplug.dll') self.hdl = self.objdll.USBPLUG_Open(1) def connectUsb(self): res = self.objdll.USBPLUG_Set(self.hdl, 1) #連接USB print("連接 USB " + self.resDict[res]) def disconnectUsb(self): res = self.objdll.USBPLUG_Set(self.hdl, 0) #斷開USB print("斷開 USB " + self.resDict[res]) def __del__(self): self.objdll.USBPLUG_Close(self.hdl) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) Form=QtGui.QWidget() main=Ui_Dialog() main.setupUi(Form) Form.show() sys.exit(app.exec_())
ok, 以上就是pyqt4的相關使用,這里應用的比較簡單,更加深入的使用還需要繼續學習。。。
二,將.py 文件打包成.exe可執行程序
這里我用到的Pyinstaller這個模塊,首先,需要安裝pyinstaller; 安裝方法推薦 使用 pip install pyinstaller(由於這個功能的實現還需要依賴一些其他的庫,pip比較省事)
安裝完成后,我們可以在如下路徑找到Pyinstaller應用程序:C:\Python27\Scripts\
參考鏈接:http://jingyan.baidu.com/article/a378c960b47034b3282830bb.html
比較直接的方法就是使用Pyinstaller應用程序調用待發布腳本
即執行:pyinstaller.exe -w -F xx\xx\xxx.py
-w: 直接發布的exe應用帶命令行調試窗口,在指令內加入-w命令可以屏蔽掉命令框(調試階段可不加-w, 最終發布時加入-w參數)
-F: 這里是大寫。使用-F指令可以把應用打包成一個獨立的exe文件,否則是一個帶各種dll和依賴文件的文件夾
-p :這個指令后面可以增加pyinstaller搜索模塊的路徑。因為應用打包涉及的模塊很多。這里可以自己添加路徑。不過經過筆者測試,site-packages目錄下都是可以被識別的,一般不需要再手動添加