Embed function buttons for graph

393 views
Skip to first unread message

geoid...@gmail.com

unread,
Dec 14, 2015, 1:07:28 PM12/14/15
to pyqtgraph
Hi guys,
I have a working graphing program with start stop and exit buttons. I'd like to know if there is simple way to have both the graph and buttons in the same window. Right now I have two separate gui windows.

I stripped the code down to just the gui elements:

#Start the Gui window
app = QtGui.QApplication([])

#Setup the plot
p = pg.plot()
p.setWindowTitle('Its about Real-Time')
#Make depths go down
p.invertY(True)
p.resize(1024, 768)
#define our curve
curve = p.plot()

#Start and Stop Buttons
class Buttons(QtGui.QMainWindow):
   
    def __init__(self):
        super(Buttons, self).__init__()
       
        self.initUI()
       
    def initUI(self):     

        btn1 = QtGui.QPushButton("Start", self)
        btn1.move(30, 50)

        btn2 = QtGui.QPushButton("Stop", self)
        btn2.move(150, 50)
       
        btn3 = QtGui.QPushButton("Exit", self)
        btn3.move(270, 50)
     
        self.statusBar()
       
        self.setGeometry(300, 300, 400, 150)
        self.setWindowTitle('Event sender')
        self.show()

#Run Button program       
bt = Buttons()       

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

chronis patapis

unread,
Dec 14, 2015, 6:23:54 PM12/14/15
to pyqtgraph
Yes it's relatively simple. First I would use a layout manager for your gui (QGridLayout, QHBoxLayout, QVBoxLayout ). Then you can create a pg.GraphicsLayoutWidget or a pg.PlotWidget and add that to your gui layout together with all other widgets that you need.

class MyApp(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
# plot widget to contain plot
plot_widget = pg.PlotWidget()
# plot item
plot = pg.PlotDataItem()
plot.setData(np.array(range(10)), np.array(range(10)))
# Vertical Box Layout
layout = QtGui.QVBoxLayout()
self.setLayout(layout)
# Some button
button = QtGui.QPushButton('Button')
# add to layout
layout.addWidget(plot_widget)
layout.addWidget(button)
# add plot item to its widget
plot_widget.addItem(plot)


qapp = QtGui.QApplication(sys.argv)
app = MyApp()
app.show()
qapp.exec_()

It is nicely explained in the docs

http://www.pyqtgraph.org/documentation/plotting.html


Hope this helps
Chronis

geoid...@gmail.com

unread,
Dec 16, 2015, 6:07:00 PM12/16/15
to pyqtgraph
Thanks that helps.
Reply all
Reply to author
Forward
0 new messages