python - Functional start and stop button in a GUI using pyqt or pyside for real time data acquisition using pyqtgraph -
i implementing program using scrollingplots example provided pyqtgraph here https://github.com/skycaptain/gazetrack/blob/master/gui/pyqtgraph/examples/scrollingplots.py
import pyqtgraph pg pyqtgraph.qt import qtcore, qtgui import numpy np win = pg.graphicswindow() win.setwindowtitle('pyqtgraph example: scrolling plots') win.nextrow() p3 = win.addplot() p4 = win.addplot() # use automatic downsampling , clipping reduce drawing load p3.setdownsampling(mode='peak') p4.setdownsampling(mode='peak') p3.setcliptoview(true) p4.setcliptoview(true) p3.setrange(xrange=[-100, 0]) p3.setlimits(xmax=0) curve3 = p3.plot() curve4 = p4.plot() data3 = np.empty(100) ptr3 = 0 def update2(): global data3, ptr3 data3[ptr3] = np.random.normal() ptr3 += 1 if ptr3 >= data3.shape[0]: tmp = data3 data3 = np.empty(data3.shape[0] * 2) data3[:tmp.shape[0]] = tmp curve3.setdata(data3[:ptr3]) curve3.setpos(-ptr3, 0) curve4.setdata(data3[:ptr3]) # update plots timer = pg.qtcore.qtimer() timer.timeout.connect(update3) timer.start(50) ## 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_()
at first wanted use ctrl+c signal stop continuous data plotting , save data obtained file. however, way quit program close graph window. executing ctrl+c in terminal not anything.
therefore, implement button start , stop(and save data) in program.
as newbie in python , object oriented programming, looked examples online. have found examples button implementation in gui:
stackoverflow.com/questions/8762870/how-to-implement-a-simple-button-in-pyqt
groups.google.com/forum/#!topic/pyqtgraph/bxvzhtb1kkg
www.youtube.com/watch?v=z33vwdhrafm , gui related tutorials youtuber
none of these examples have aided me in achieving want not know how combine them scrollingplots example.
from qt crash course webpage (pyqtgraph.org/documentation/qtcrashcourse.html):
from pyqt4 import qtgui # (the example applies equally pyside) import pyqtgraph pg ## start initializing qt (only once per application) app = qtgui.qapplication([]) ## define top-level widget hold w = qtgui.qwidget() ## create widgets placed inside btn = qtgui.qpushbutton('press me') text = qtgui.qlineedit('enter text') listw = qtgui.qlistwidget() plot = pg.plotwidget() ## create grid layout manage widgets size , position layout = qtgui.qgridlayout() w.setlayout(layout) ## add widgets layout in proper positions layout.addwidget(btn, 0, 0) # button goes in upper-left layout.addwidget(text, 1, 0) # text edit goes in middle-left layout.addwidget(listw, 2, 0) # list widget goes in bottom-left layout.addwidget(plot, 0, 1, 3, 1) # plot goes on right side, spanning 3 rows ## display widget new window w.show() ## start qt event loop app.exec_()
as there app.exec_() @ end of of button example codes , there , update loop in scrollingplot example itself, confused how can run @ same time.
i have read somewhere regarding such using gui continuously running process, should consider using timer or multithreading. nevertheless, presently not have knowledge in threading.
i have tried tkinter found guide on how use tkinter matplotlib -> pythonprogramming.net/how-to-embed-matplotlib-graph-tkinter-gui/
looking forward receiving advice regarding problem.
from pyqt4 import qtcore, qtgui import pyqtgraph pg class mainform(qtgui.qmainwindow): def __init__(self): super(mainform, self).__init__() self.playtimer = qtcore.qtimer() self.playtimer.setinterval(500) self.playtimer.timeout.connect(self.playtick) self.toolbar = self.addtoolbar("play") self.playscansaction = qtgui.qaction(qtgui.qicon("control_play_blue.png"), "play scans", self) self.playscansaction.triggered.connect(self.playscanspressed) self.playscansaction.setcheckable(true) self.toolbar.addaction(self.playscansaction) def playscanspressed(self): if self.playscansaction.ischecked(): self.playtimer.start() else: self.playtimer.stop() def playtick(self): pass def main(): app = qtgui.qapplication(sys.argv) form = mainform() form.initui("scan log display") form.show() app.exec_() if __name__ == "__main__": main()
Comments
Post a Comment