[PyQt] How to manage loops with button clicks in PyQt5?

binsha s binshasrn at gmail.com
Tue Mar 26 08:25:37 GMT 2019


I have text file to read the values and plot a graph as a slideshow. For
that I split the data into n sets for the graph to show as a slideshow. I
have two list containing values as:

x_data:  [array([[-210. ],[-209.98], [-209.96],[82.54]]),array([[-82.5
],[44.96],[44.98]]),...array([[300.  ],[300.02]])

y_data:   [array([[-10.01953],[ -1.93359],[
-5.33203],...[-10.10742]]),array([[-10.07812],[-10.07812]]),...array([[-19.6875],
[-19.6875]])] .

Six buttons are there to interrupt the slideshow graph. play ,pause,
forward_multiple, backward_multiple,forward_one, backward_one

Actions required by the click of buttons are as follows:

   -

   *Play button:* When I click play button the graph is plotting based on
   the array values of x_data and y_data. (It is working)
   -

   *Pause Button:* Should stop currently running graph (Not working)
   -

   *Forward one:* Should play forward one step after the current graph.
   -

   *Forward multiple:* Plot graphs from the current set till the last set
   in the array. ie forward play from current graph till last graph.(Works,
   but when it reaches the last graph it exit from the app)
   -

   *Backward one*: Play one step backwards from the current graph
   playing.(Works, works for one time only. If I click again, not showing the
   previous graph)
   -

   *Backward multiple:* Play backwards from the current graph running till
   the starting graph.(Works, but it is looping continuously. I want to play
   up-to the starting graph)

What I have tried is:

class MatplotlibWidget(QMainWindow):
    def __init__(self):
        ---
        self.playbutton.clicked.connect(self.selectFile)
        self.pause_button.clicked.connect(self.stopRun)
        self.backward_btn.clicked.connect(self.backward)
        self.forward_btn.clicked.connect(self.forward)
        self.backward_multiple.clicked.connect(self.backward_multi)
        self.forward_multiple.clicked.connect(self.forward_multi)
        ---
def drawGraph(self,i):

    global  x,y,iters,val,gvalue,x_data,y_data,j,n_graphs,sec_value
    # Reading x and y values from file
    x = data[:, 0].reshape(m, 1)
    y = data[:, col_value-1].reshape(m, 1)
    gvalue=self.graphSpinbox.value()
    iters=m //(gvalue-1)
    global current_iter
    current_iter = 0
    val=self.timeinterval_spinbox.value()
    sec_value = str(val) + str(0) + str(0) + str(0)
    x_data = []
    y_data = []
    n_graphs=[]
    for i in range(gvalue):
        x_data.append(x[current_iter:current_iter+iters])
        y_data.append(y[current_iter:current_iter+iters])
        current_iter=current_iter+iters
    for j in range(gvalue):
        n_graphs.append(str(j + 1))
        self.plotDraw(x_data[j],y_data[j])
        j=j+1
def plotDraw(self,x ,y):

    self.MplWidget.canvas.axes.clear()
    self.MplWidget.canvas.axes.plot(x, y)
    self.MplWidget.canvas.axes.legend(('cosinus', 'sinus'), loc='upper right')
    self.MplWidget.canvas.axes.set_title('Signal')
    self.MplWidget.canvas.draw()
    loop = QEventLoop()
    QTimer.singleShot(int(sec_value), loop.quit)
    loop.exec_()
def stopRun(self):

    loop = QEventLoop()
    QTimer.singleShot(int(sec_value), loop.quit)
    loop.exec_()
    self.event_source.stop()
def backward(self):
    curr_pos = int(n_graphs[len(n_graphs) - 1])
    print("n_graphs: ",n_graphs)
    print("curr_pos: ",curr_pos)
    if curr_pos!=0:
        self.plotDraw(x_data[curr_pos-2], y_data[curr_pos-2])
def backward_multi(self):
    curr_pos = int(n_graphs[len(n_graphs) - 1])
    print("n_graphs: ", n_graphs)
    print("curr_pos: ", curr_pos)
    while curr_pos<=gvalue:
        self.plotDraw(x_data[curr_pos - 2], y_data[curr_pos - 2])
        curr_pos=curr_pos-1
        loop = QEventLoop()
        QTimer.singleShot(int(sec_value), loop.quit)
        loop.exec_()
def forward(self):
    print("Forward button clicked")
    print(n_graphs)
    curr_pos=int(n_graphs[len(n_graphs)-1])
    print(curr_pos)
    if curr_pos!=0:
        self.plotDraw(x_data[curr_pos],y_data[curr_pos])

def forward_multi(self):
    print(n_graphs)
    curr_pos = int(n_graphs[len(n_graphs) - 1])
    print(curr_pos)
    while curr_pos<=gvalue:
        self.plotDraw(x_data[curr_pos], y_data[curr_pos])
        curr_pos = curr_pos + 1
        loop = QEventLoop()
        QTimer.singleShot(int(sec_value), loop.quit)
        loop.exec_()

How to manage this button interrupts?. I will be thankful, if anyone helps
me to fix this.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190326/d64475d4/attachment-0001.html>


More information about the PyQt mailing list