# -*- coding: utf-8 -*- """ This example demonstrates the use of RemoteGraphicsView to improve performance in applications with heavy load. It works by starting a second process to handle all graphics rendering, thus freeing up the main process to do its work. In this example, the update() function is very expensive and is called frequently. After update() generates a new set of data, it can either plot directly to a local plot (bottom) or remotely via a RemoteGraphicsView (top), allowing speed comparison between the two cases. IF you have a multi-core CPU, it should be obvious that the remote case is much faster. """ from PyQt5.QtCore import QObject,pyqtSignal,QThread,pyqtSlot from pyqtgraph.Qt import QtGui, QtCore import pyqtgraph as pg import pyqtgraph.widgets.RemoteGraphicsView import numpy as np import mylabjack import sys import tkinter from tkinter import filedialog # Print screen from PIL import Image,ImageGrab import datetime import time class window(): def __init__(self): self.theDict=dict() self.layout = pg.LayoutWidget() self.AIN0View = pg.widgets.RemoteGraphicsView.RemoteGraphicsView() self.AIN0View.pg.setConfigOptions(antialias=True) ## prettier plots at no cost to the main process! self.AIN1View = pg.widgets.RemoteGraphicsView.RemoteGraphicsView() self.AIN1View.pg.setConfigOptions(antialias=True) ## prettier plots at no cost to the main process! self.AIN13View = pg.widgets.RemoteGraphicsView.RemoteGraphicsView() self.AIN13View.pg.setConfigOptions(antialias=True) ## prettier plots at no cost to the main process! self.DIO0View = pg.widgets.RemoteGraphicsView.RemoteGraphicsView() self.DIO0View.pg.setConfigOptions(antialias=True) ## prettier plots at no cost to the main process! theRow=0 self.layout.addWidget(self.AIN0View, row=theRow, col=0) theRow+=1 self.layout.addWidget(self.AIN1View, row=theRow, col=0) theRow+=1 self.layout.addWidget(self.AIN13View, row=theRow, col=0) theRow+=1 self.layout.addWidget(self.DIO0View, row=theRow, col=0) theRow+=1 self.AIN0Plt = self.AIN0View.pg.PlotItem() self.AIN0Plt._setProxyOptions(deferGetattr=True) ## speeds up access to rplt.plot self.AIN0View.setCentralItem(self.AIN0Plt) self.AIN1Plt = self.AIN1View.pg.PlotItem() self.AIN1Plt._setProxyOptions(deferGetattr=True) ## speeds up access to rplt.plot self.AIN1View.setCentralItem(self.AIN1Plt) self.AIN13Plt = self.AIN13View.pg.PlotItem() self.AIN13Plt._setProxyOptions(deferGetattr=True) ## speeds up access to rplt.plot self.AIN13View.setCentralItem(self.AIN13Plt) self.DIO0Plt = self.DIO0View.pg.PlotItem() self.DIO0Plt._setProxyOptions(deferGetattr=True) ## speeds up access to rplt.plot self.DIO0View.setCentralItem(self.DIO0Plt) #self.layout.resize(800,800) self.layout.show() self.spectrumObject=spectrum() self.spectrumObject.acquisitionDone.connect(self.updateSpectrum) self.spectrumObject.start() def updateSpectrum(self,ain): #print(ain) if "trigger" in self.theDict: #this is just for labjack debugging purposes, and can e safely commented if self.theDict["trigger"][0]!=1: thePrintScreen=ImageGrab.grab() now=datetime.datetime.now() fileName="%s.%s.%sa%s.%s%s.png"%(now.strftime("%y"),now.strftime("%m"),now.strftime("%d"),now.strftime("%H"),now.strftime("%M"),now.strftime("%S")) thePrintScreen.save(fileName) AIN0=ain[0,:] AIN1=ain[1,:] AIN13=ain[2,:] DIO0=ain[3,:] DIO0[DIO0==255]=1 DIO0[DIO0==254]=0 self.AIN0Plt.plot(AIN0, clear=True, _callSync='off') ## We do not expect a return value. ## By turning off callSync, we tell ## the proxy that it does not need to ## wait for a reply from the remote ## process. self.AIN1Plt.plot(AIN1, clear=True, _callSync='off') self.AIN13Plt.plot(AIN13, clear=True, _callSync='off') self.DIO0Plt.plot(DIO0, clear=True, _callSync='off') self.theDict=dict() self.theDict["piezoVoltage"]=AIN0 self.theDict["photoDiodeVoltage"]=AIN1 self.theDict["LaserTempToSetPointVoltage"]=AIN13 self.theDict["trigger"]=DIO0 self.spectrumObject.start() def updateImage(self,image): #print(ain) self.imv.setImage(image) self.imageObject.start() class spectrum(QThread): acquisitionDone = pyqtSignal(object) redo = pyqtSignal() def __init__(self): QThread.__init__(self) self.handle=mylabjack.openLabJack() self.changeTemp(25) self.redo.connect(self.run) def __del__(self): self.wait() mylabjack.closeLabJack(self.handle) def run(self): ain=mylabjack.acquireSpectrum(self.handle) self.acquisitionDone.emit(ain) @pyqtSlot(float) def changeTemp(self,newTemp): mylabjack.updateVoltage(self.handle,mylabjack.tempToVoltage(newTemp),0) ## Start Qt event loop unless running in interactive mode or using pyside. if __name__ == '__main__': #import sys #if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): # QtGui.QApplication.instance().exec_() app = QtGui.QApplication(sys.argv) mywimdow = window() #form.show() app.exec_()