Functional start and stop button in a GUI using pyqt or pyside for real time data acquisition using pyqtgraph
Question: I am implementing my program using the scrollingplots example provided by
pyqtgraph here
<https://github.com/skycaptain/gazetrack/blob/master/gui/pyqtgraph/examples/scrollingPlots.py>

    
    
    import pyqtgraph as pg
    from pyqtgraph.Qt import QtCore, QtGui
    import numpy as np
    
    win = pg.GraphicsWindow()
    win.setWindowTitle('pyqtgraph example: Scrolling Plots')
    win.nextRow()
    p3 = win.addPlot()
    p4 = win.addPlot()
    # Use automatic downsampling and clipping to reduce the 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 all 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 I wanted to use Ctrl+C as a signal to stop the continuous data
plotting and save the data obtained into a file. However, the only way to quit
the program is to close the graph window. Executing Ctrl+C in the terminal
does not do anything.

Therefore, I would like to implement a button to start and stop(and save the
data) in the program.

As a newbie in Python and Object Oriented Programming, I looked for examples
online. I have found examples specifically for the button implementation in
the 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 and GUI related tutorials by the youtuber

None of these examples have aided me in achieving what I want as I do not know
how to combine them with the scrollingplots example.

From Qt crash course webpage (pyqtgraph.org/documentation/qtcrashcourse.html):

    
    
    from PyQt4 import QtGui  # (the example applies equally well to PySide)
    import pyqtgraph as pg
    
    ## Always start by initializing Qt (only once per application)
    app = QtGui.QApplication([])
    
    ## Define a top-level widget to hold everything
    w = QtGui.QWidget()
    
    ## Create some widgets to be placed inside
    btn = QtGui.QPushButton('press me')
    text = QtGui.QLineEdit('enter text')
    listw = QtGui.QListWidget()
    plot = pg.PlotWidget()
    
    ## Create a grid layout to manage the widgets size and position
    layout = QtGui.QGridLayout()
    w.setLayout(layout)
    
    ## Add widgets to the layout in their 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 the widget as a new window
    w.show()
    
    ## Start the Qt event loop
    app.exec_()
    

As there will app.exec_() at the end of most of the button example codes and
there is also and update loop in the scrollingplot example itself, I am
confused as to how they can run at the same time.

I have read somewhere that regarding such using gui for a continuously running
process, I should consider using timer or multithreading. Nevertheless, I
presently do not have any knowledge in threading.

I have even tried Tkinter as I found some a guide on how to use Tkinter with
matplotlib -> pythonprogramming.net/how-to-embed-matplotlib-graph-tkinter-gui/

Looking forward to receiving any advice regarding this problem.


Answer: 
    from PyQt4 import QtCore, QtGui
    import pyqtgraph as 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()
    

