How to add stop button to pyqtgraph?
-
I have a program that launches a graph using pyqtgraphing as a pyside2 qt application. What I want to happen is to have the user hit a button and that makes the graph stop. I am setting up the graph like this
def set_up_graph(self): QApplication.setAttribute(QtCore.Qt.AA_Use96Dpi) pg.setConfigOption('background', 'w') pg.setConfigOption('foreground', 'k') plot = pg.plot() plot.setLabel('bottom', 'Frequency') plot.setLabel('left', 'Power Value') plot.setWindowTitle('Frequency vs Power Graph')
And then I am actually doing the graphing using this function
def plot_the_graph(self): power_vals = [1,2,3,4] frequency_values = [5,6,7,8] plot.plot(frequency_values, power_vals, clear=True, pen=pg.mkPen('g', width=1.5), symbol='x', name="Power Vs. Frequency") QtGui.QGuiApplication.processEvents()
And then I have one other function that determines what button the user has hit
def multi_graph(self): if self.multi: self.plot_the_graph() self.multi_graph()
I want to add a stop button to these graphs so then I can have it be when the user hits stop then self.multi = False, and then the graph would stop. I can't find a way to just add a stop button to this though.
Currently what's happening is that the graph will just run and run and then eventually I will have 60 popup windows of the graph and I will have to force stop it.
-
@poordev123 said in How to add stop button to pyqtgraph?:
plot = pg.plot()
This is local variable in set_up_graph(self).
So, it is not hte same used in plot_the_graph(self).I never used PyQtgraph but according to its documentation you should rather do like this:
pw = pg.plot(xVals, yVals, pen='r') # plot x vs y in red pw.plot(xVals, yVals2, pen='b') win = pg.GraphicsWindow() # Automatically generates grids with multiple items win.addPlot(data1, row=0, col=0) win.addPlot(data2, row=0, col=1) win.addPlot(data3, row=1, col=0, colspan=2) pg.show(imageData
instead of calling static plot() which creates a new plot at each call.
I suggest you spend some time to read:
https://pyqtgraph.readthedocs.io/en/latest/how_to_use.html
as it is not part of Qt.