2

我有一个访问外部 LabJack 设备的类。Labjack 设备通过 USB 连接,其主要功能是通过 python 命令打开或关闭某些东西。我想将异常传递给我的 pyqt 主窗口应用程序,以防万一 labjack 未连接。我不完全确定我的 LabJack 课程如何:

import u3

class LabJack:
    def __init__(self):

        try:
            self.Switch = u3.U3()
        except:
            print "Labjack Error"

        #Define State Registers for RB12 Relay Card

        self.Chan0 = 6008
        Chan1 = 6009
        Chan2 = 6010
        Chan3 = 6011
        Chan4 = 6012
        Chan5 = 6013

#Turn the channel on
    def IO_On(self,Channel):
        self.Switch.writeRegister(Channel,0)


    #Turn the channel off
    def IO_Off(self,Channel):   
        self.Switch.writeRegister(Channel,1)

    #The State of the Channel
    def StateSetting(self,Channel):
        self.Switch.readRegister(Channel)
        if Switch.readRegister(Channel) == 0:
            print ('Channel is On')
        else:
            print('Channel is Off')

    #Direction of Current Flow
    def CurrentDirection(self,Channel):
        self.Switch.readRegister(6108)
        print self.Switch.readRegister(6108)

这是我的pyqt代码:

import re
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from LabJackIO import *
from Piezo902 import *
import functools

import ui_aldmainwindow

class ALDMainWindow(QMainWindow,ui_aldmainwindow.Ui_ALDMainWindow):

    def __init__(self, parent=None):
        super(ALDMainWindow,self).__init__(parent) 
        self.setupUi(self)

        self.ValveControl = LabJack()



        self.Valve_ON.clicked.connect(functools.partial(self.ValveControl.IO_On,self.ValveControl.Chan0))
        self.Valve_OFF.clicked.connect(functools.partial(self.ValveControl.IO_Off,self.ValveControl.Chan0))
        self.statusBar().showMessage('Valve Off')

app = QApplication(sys.argv)
app.setStyle('motif')
form = ALDMainWindow()
form.show()
app.exec_()

有什么建议么?

谢谢。

4

1 回答 1

0

如果您LabJack可以从您那里继承,QObject则可以在捕获异常时发出信号:

class LabJack(QtCore.QObject):
    switchFailed = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(LabJack, self).__init__(parent)
        try:
            self.Switch = u3.U3()

        except:
            self.switchFailed.emit()

然后在您的内部设置一个插槽ALDMainWindow来处理异常:

        self.ValveControl = LabJack()
        self.ValveControl.switchFailed.connect(self.on_ValveControl_switchFailed)

    @QtCore.pyqtSlot()
    def on_ValveControl_switchFailed(self):
        print "Handle exception here."
于 2013-09-24T04:23:55.913 回答