1. 首先使用QT(具体怎么安装自行查哦)的Designer手动编辑一个类似如下的的UI界面;
然后把UI_DEMO保存到一个指定的文件夹中;
2. 在PyCharm中使用PyUIC工具来把UI_DEMO转成UI_DEMO.py文件;
2.1 如何配置PyUIC请参考如下所示(File-Settings-Tool-Extenal tools):
3. 编写UI_Main 主窗口文件,来应用UI_DEMO.py,代码如下所示:
如下示例程序涉及模块较多,有Button、Label、Edit、Progress、Dialog、Thread、configParser(Ini)、PyQt5、ICO以及一些 事件的调用等,等于说学会了如下示例,对一些刚入门的同学来说,应付公司的一些基本的功能需求是没有问题的。
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import os, sys, configparser
from threading import Thread
try:
from ui.UI_DEMO import UI_DEMO
except Exception as e:
print(e)
SETTINGS = 'Settings'
# 注意引入的UI
class hciLogTool(QDialog, UI_DEMO):
def __init__(self, parent=None):
super(hciLogTool, self).__init__(parent)
self.setupUi(self)
self.setWindowTitle("Parser V1.0")
self.initUI()
# 初始UI相关,绑定控件
def initUI(self):
# 可注释掉
icon = QIcon()
icon.addPixmap(QPixmap("images/logo.ico"), QIcon.Normal)
self.setWindowIcon(icon)
self.lineEditHCILog.setReadOnly(True)
self.lineEditTimeStamp.setReadOnly(True)
self.progressBar.hide()
self.progressBar.setValue(0)
self.pushButtonHCI.clicked.connect(self.clickHCILogPath)
self.pushButtonStamp.clicked.connect(self.clickTimeStampPath)
self.buttonBox.button(QDialogButtonBox.Ok).setText('Parse')
# 配置文件相关
self.cf = configparser.ConfigParser()
self.cf.read('fileList.ini')
if not self.cf.has_section(SETTINGS):
self.cf.add_section(SETTINGS)
try:
self.lineEditHCILog.setText(self.getEntry('LOG'))
self.lineEditTimeStamp.setText(self.getEntry('STAMP'))
except Exception as e:
print(e)
# 点击click button 事件1
def clickHCILogPath(self):
curPath = self.lineEditHCILog.text()
if not os.path.isfile(curPath):
curPath = '.'
fileName, fileType = QFileDialog.getOpenFileName(self,
"Select Log File",
curPath,
"Binary Files (*.bin);;All Files (*)")
if len(fileName):
self.lineEditHCILog.setStyleSheet("Color:Black")
self.lineEditHCILog.setText(fileName)
self.setEntry("LOG", fileName)
# 点击click button 事件2
def clickTimeStampPath(self):
curPath = self.lineEditTimeStamp.text()
if not os.path.isfile(curPath):
curPath = '.'
fileName, fileType = QFileDialog.getOpenFileName(self,
"Select Time Stamp File",
curPath,
"Text Files (*.txt);;All Files (*)")
if len(fileName):
self.lineEditTimeStamp.setStyleSheet("Color:Black")
self.lineEditTimeStamp.setText(fileName)
self.setEntry("STAMP", fileName)
# 存储配置到文件中,
def setEntry(self, entry, value):
# print("{} =".format(entry), value)
self.cf.set(SETTINGS, entry, value)
self.cf.write(open('fileList.ini', 'w+'))
# 获取配置文件中的信息
def getEntry(self, entry):
return self.cf.get(SETTINGS, option=entry)
# 设置进度条
def setProgress(self, pos):
self.progressBar.setValue(pos)
# 使能/失能 控件
def enableControl(self, enable=True):
self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(enable)
self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enable)
self.pushButtonHCI.setEnabled(enable)
self.pushButtonStamp.setEnabled(enable)
# 线程实体,如果执行实体是一个比较耗时的逻辑
# 长时间会导致主窗口存在卡顿问题,所示加入线程
def convertCfg(self):
logFile = self.lineEditHCILog.text()
timeStampFile = self.lineEditTimeStamp.text()
cfgFile = ''.join([os.path.splitext(logFile)[0], '.cfg'])
self.progressBar.show()
self.enableControl(False)
if os.path.isfile(logFile) and os.path.isfile(timeStampFile):
if os.path.isfile(cfgFile): os.remove(cfgFile)
bt = btsnoop.btsnoop_encoder(logFile, timestamp=timeStampFile,
output=cfgFile,
progress=self.setProgress)
if os.path.isfile(cfgFile):
self.progressBar.hide()
self.enableControl()
# 启动线程
def accept(self):
self.handleThread = Thread(target=self.convertCfg)
self.handleThread.start()
# 启动主窗口程序
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = hciLogTool()
ex.show()
sys.exit(app.exec_())
运行结果如下所示: