功能需求
需求分解需求
- 初始化
- 吸
- 吐
- 排空
界面布局
布局- 将界面ui文件编译为py文件
- 利用pyuic5工具进行,pyuic5 -o ui_xxx.py xxx.ui
- 编译后的ui_xxx.py文件实际上是一个数据类
- 在该类里有创建窗口控件的代码和控件的属性配置数据
提升控件
- 现有的控件不足以表示注射泵的状态
-
需要自定义控件,Qt提供了promote(提升)操作
image.png
image.png - 这样就会有一个提升类Injector,这个控件就会是Injector的实例
- 在Python中实现这个类
资源
- 界面上需要用到一些图片等资源
- 这些资源在QT中有专门的resource系统
- 创建一个qrc文件,在文件中描述需要引用的资源
<RCC>
<qresource prefix="/res">
<file alias="v0">input.fw.png</file>
<file alias="v120">ManualPurge.fw.png</file>
<file alias="v240">output.fw.png</file>
<file alias="inj">planger.fw.png</file>
<file>megarobo.ico</file>
</qresource>
</RCC>
- 这个资源文件也需要被编译为py数据文件
- pyrcc5 -o xxx.py xxx.qrc
- 对资源项目可以取个别名,便于在程序中通过状态码来得到资源数据
-
几张图片,分别表示注释器和阀门的3种状态,从而组合出注射泵的工作状态
planger.fw.png
技术栈
- pyQt
- mega studio
引入包
- 系统包
- pyQt相关包
- injector模块:自定义的注射泵状态控件
- 界面模块:利用QtDesigner设计的form
- 界面模块需要利用pyuic5.exe进行编译,转换为py数据文件
- 资源模块:
- 利用pyrcc5进行编译
- 资源中含有一些图片和图片的别名
- mrq.inj2
- 这是MEGAROBO对注射泵的Python封装
import sys
import time
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import injector
import ui_mrx_inj2
import qrc_mrx_inj2
import mrq.inj2
创建窗口类
class mrxInj2( QWidget, ui_mrx_inj2.Ui_Form ):
def __init__( self, parent = None):
创建设备
def onConnect( self ):
self.mInj2 = mrq.inj2.Inj2( self.edtDevice.text() )
self.labIdn.setText( self.mInj2.IDN )
self.validateUi()
事件驱动
- 事件发生后,调用设备的相关方法进行响应
- 为按钮添加消息处理函数
- 例如“阀门”切换到“吸入”状态
- 控制电机转动
def onLeft( self ):
if ( self.mValve == 0 ):
pass
elif ( self.mValve == 120 ):
self.mInj2.rotate( 0,0, self.mValveSepAngle, self.mValveSepTime )
self.setValve( 0 )
else:
self.mInj2.rotate( 0,0, -self.mValveSepAngle, self.mValveSepTime )
self.setValve( 0 )
建立事件和响应的联系
def buildConnection( self ):
self.btnValveCcw.clicked.connect( self.onLeft )
创建界面,启动消息循环
app = QApplication( sys.argv )
inj2 = mrxInj2()
inj2.show()
app.exec_()
漂亮一点
- 为界面添加风格样式
fo = open("./mega.qss", "r")
dat = fo.read()
fo.close()
app.setStyleSheet( dat )
添加风格
代码
#! coding:utf-8
import sys
import time
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import injector
import ui_mrx_inj2
import qrc_mrx_inj2
import mrq.inj2
class mrxInj2( QWidget, ui_mrx_inj2.Ui_Form ):
def __init__( self, parent = None):
super( mrxInj2, self ).__init__( parent )
self.setupUi( self )
self.buildConnection()
self.setWindowIcon( QIcon(":/res/megarobo.ico") )
self.init()
# members
self.mInj2 = None
self.mInjMax = 60
self.mValveSepAngle = 120 # sep angle
self.mValveSepTime = 0.5
self.validateUi()
self.setInj( 0 )
self.setValve( 0 )
def buildConnection( self ):
self.btnValveCw.clicked.connect( self.onRight )
self.btnValveCcw.clicked.connect( self.onLeft )
self.btnManualPurge.clicked.connect( self.onManualPurg )
self.btnSuction.clicked.connect( self.onSuction )
self.btnInjection.clicked.connect( self.onInjection )
self.btnConnect.clicked.connect( self.onConnect )
self.btnDisconnect.clicked.connect( self.onDisconnect )
self.btnCenter.clicked.connect( self.onCenter )
self.btnPurging.clicked.connect( self.onPurging )
# self.sldInj.valueChanged.connect( self.onInjValue )
# right
# 0 240 120
# \ \ / /
# \ \/ /
# | |
# | |
# 0 120 240
def onRight( self ):
if ( self.mValve == 0 ):
self.mInj2.rotate( 0,0, self.mValveSepAngle, self.mValveSepTime )
self.setValve( 240 )
elif ( self.mValve == 120 ):
self.mInj2.rotate( 0,0, -self.mValveSepAngle, self.mValveSepTime )
self.setValve( 240 )
else:
pass
# left
def onLeft( self ):
if ( self.mValve == 0 ):
pass
elif ( self.mValve == 120 ):
self.mInj2.rotate( 0,0, self.mValveSepAngle, self.mValveSepTime )
self.setValve( 0 )
else:
self.mInj2.rotate( 0,0, -self.mValveSepAngle, self.mValveSepTime )
self.setValve( 0 )
# manual purge
def onManualPurg(self):
if ( self.mValve == 0 ):
self.mInj2.rotate( 0,0, -self.mValveSepAngle, self.mValveSepTime )
self.setValve( 120 )
elif ( self.mValve == 240 ):
self.mInj2.rotate( 0,0, self.mValveSepAngle, self.mValveSepTime )
self.setValve( 120 )
else:
pass
def onSuction( self ):
dLen = self.mInjMax - self.mInj
if ( dLen > self.spinSucLen.value() ):
dLen = self.spinSucLen.value()
else:
pass
self.mInj2.line( 0,0, dLen, self.spinSucTime.value() )
self.setInj( dLen + self.mInj )
def onInjection( self ):
if ( self.mInj > self.spinInjLen.value() ):
dLen = self.spinInjLen.value()
else:
dLen = self.mInj
self.mInj2.line( 0,0, -dLen, self.spinInjTime.value() )
self.setInj( self.mInj - dLen )
def onConnect( self ):
self.mInj2 = mrq.inj2.Inj2( self.edtDevice.text() )
self.labIdn.setText( self.mInj2.IDN )
self.validateUi()
def onDisconnect( self ):
self.mInj2.close()
self.mInj2 = None
self.validateUi()
def onCenter( self ):
self.mInj2.center()
self.init()
def init( self ):
self.setInj( 0 )
self.setValve( 0 )
# valve
def setValve( self, deg ):
self.mValve = deg
self.widgetInj.setAngleIndex( deg )
def setInj( self, depth ):
self.lcdInj.display( depth )
self.mInj = depth
self.widgetInj.setLen( depth )
def validateUi( self ):
if ( self.mInj2 == None ):
bValidate = False
else:
bValidate = True
self.spinSucTime.setEnabled( bValidate )
self.spinSucLen.setEnabled( bValidate )
self.spinInjTime.setEnabled( bValidate )
self.spinInjLen.setEnabled( bValidate )
self.spinPurgCnt.setEnabled( bValidate )
self.btnValveCcw.setEnabled( bValidate )
self.btnValveCw.setEnabled( bValidate )
self.btnManualPurge.setEnabled( bValidate )
self.btnSuction.setEnabled( bValidate )
self.btnInjection.setEnabled( bValidate )
self.btnConnect.setEnabled( not bValidate )
self.btnDisconnect.setEnabled( bValidate )
self.btnCenter.setEnabled( bValidate )
self.btnPurging.setEnabled( bValidate )
def cbProcess( self ):
QApplication.processEvents()
def onPurging( self):
#! program
self.mInj2.purge( 0, 0,
self.mValveSepAngle,
self.spinPurgeLength.value(),
self.spinPurgeTime.value() )
self.mInj2.waitIdle( 0, 0, 60, self.cbProcess )
#! call
# for i in range( 1, self.spinPurgCnt.value() ):
loopCnt = self.spinPurgCnt.value() - 1
if ( loopCnt > 0 ):
self.mInj2.call( 0, 0, loopCnt )
self.mInj2.waitIdle( 0, 0, loopCnt * self.spinPurgeTime.value() * 2, self.cbProcess )
if __name__ == "__main__":
app = QApplication( sys.argv )
try:
fo = open("./mega.qss", "r")
dat = fo.read()
fo.close()
app.setStyleSheet( dat )
except IOError as e:
print( "fail to load qss")
pass
inj2 = mrxInj2()
inj2.show()
app.exec_()
网友评论