抓取设备连接类中的异常并将其传递给pyQT主窗口

时间:2013-09-23 20:57:12

标签: python pyqt

我有一个访问外部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_()

有什么建议吗?

感谢。

1 个答案:

答案 0 :(得分: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."
相关问题