功能需求
分解需求
- 初始化
- 吸
- 吐
- 排空
界面布局
- 將界面ui文件編譯為py文件
- 利用pyuic5工具進(jìn)行财岔,pyuic5 -o ui_xxx.py xxx.ui
- 編譯后的ui_xxx.py文件實(shí)際上是一個(gè)數(shù)據(jù)類
- 在該類里有創(chuàng)建窗口控件的代碼和控件的屬性配置數(shù)據(jù)
提升控件
- 現(xiàn)有的控件不足以表示注射泵的狀態(tài)
-
需要自定義控件时鸵,Qt提供了promote(提升)操作
- 這樣就會(huì)有一個(gè)提升類Injector,這個(gè)控件就會(huì)是Injector的實(shí)例
- 在Python中實(shí)現(xiàn)這個(gè)類
資源
- 界面上需要用到一些圖片等資源
- 這些資源在QT中有專門的resource系統(tǒng)
- 創(chuàng)建一個(gè)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>
- 這個(gè)資源文件也需要被編譯為py數(shù)據(jù)文件
- pyrcc5 -o xxx.py xxx.qrc
- 對(duì)資源項(xiàng)目可以取個(gè)別名,便于在程序中通過狀態(tài)碼來得到資源數(shù)據(jù)
-
幾張圖片,分別表示注釋器和閥門的3種狀態(tài)趁餐,從而組合出注射泵的工作狀態(tài)
技術(shù)棧
- pyQt
- mega studio
引入包
- 系統(tǒng)包
- pyQt相關(guān)包
- injector模塊:自定義的注射泵狀態(tài)控件
- 界面模塊:利用QtDesigner設(shè)計(jì)的form
- 界面模塊需要利用pyuic5.exe進(jìn)行編譯散怖,轉(zhuǎn)換為py數(shù)據(jù)文件
- 資源模塊:
- 利用pyrcc5進(jìn)行編譯
- 資源中含有一些圖片和圖片的別名
- mrq.inj2
- 這是MEGAROBO對(duì)注射泵的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
創(chuàng)建窗口類
class mrxInj2( QWidget, ui_mrx_inj2.Ui_Form ):
def __init__( self, parent = None):
創(chuàng)建設(shè)備
def onConnect( self ):
self.mInj2 = mrq.inj2.Inj2( self.edtDevice.text() )
self.labIdn.setText( self.mInj2.IDN )
self.validateUi()
事件驅(qū)動(dòng)
- 事件發(fā)生后菇绵,調(diào)用設(shè)備的相關(guān)方法進(jìn)行響應(yīng)
- 為按鈕添加消息處理函數(shù)
- 例如“閥門”切換到“吸入”狀態(tài)
- 控制電機(jī)轉(zhuǎn)動(dòng)
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 )
建立事件和響應(yīng)的聯(lián)系
def buildConnection( self ):
self.btnValveCcw.clicked.connect( self.onLeft )
創(chuàng)建界面,啟動(dòng)消息循環(huán)
app = QApplication( sys.argv )
inj2 = mrxInj2()
inj2.show()
app.exec_()
漂亮一點(diǎn)
- 為界面添加風(fēng)格樣式
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_()