Python Forum
PyQt + Matplotlib - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: PyQt + Matplotlib (/thread-36790.html)

Pages: 1 2


RE: PyQt + Matplotlib - Axel_Erfurt - Apr-01-2022

You could integrate the NavigationToolbar as a (movable) QToolbar

import sys
import matplotlib
matplotlib.use('Qt5Agg')
 
from PyQt5 import QtCore, QtGui, QtWidgets
 
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
 
 
class MplCanvas(FigureCanvasQTAgg):
 
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)
 
 
class MainWindow(QtWidgets.QMainWindow):
 
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        
        sc = MplCanvas(self, width=5, height=4, dpi=100)
        sc.axes.plot([0,1,2,3,4], [10,1,20,3,40])
 
        # Create toolbar, passing canvas as first parament, parent (self, the MainWindow) as second.
        
        self.t_bar = self.addToolBar("File")
        self.t_bar.setContextMenuPolicy(QtCore.Qt.PreventContextMenu)
        self.t_bar.setMovable(False)
        self.t_bar.setAllowedAreas(QtCore.Qt.TopToolBarArea)
        
        self.action_one  = QtWidgets.QAction(self.style().standardIcon(QtWidgets.QStyle.SP_DirOpenIcon), 
                                        "One", triggered = self.on_action_one)
        self.t_bar.addAction(self.action_one)
        
        self.action_two  = QtWidgets.QAction(self.style().standardIcon(QtWidgets.QStyle.SP_DriveNetIcon), 
                                        "Two", triggered = self.on_action_two)
        self.t_bar.addAction(self.action_two)
        
        self.addToolBarBreak()
        
        self.sc_bar = self.addToolBar("SC")
        self.sc_bar.addWidget(NavigationToolbar(sc, None))
         
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(sc)
 
        # Create a placeholder widget to hold our toolbar and canvas.
        widget = QtWidgets.QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)
 
        self.show()
        
    def on_action_one(self):
        print("on_action_one")

    def on_action_two(self):
        print("on_action_two")
        
 
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
app.exec_()



RE: PyQt + Matplotlib - frohr - Apr-02-2022

Thank you, I have made great progress.

I have inactive button as value indicator (I just like the style).
I defined button in buttonbox2:

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
        self.setCentralWidget(self.canvas)
        self.setMinimumSize(1024,600)   
        toolbar = NavigationToolbar(self.canvas, self)
        layout = QtWidgets.QVBoxLayout()
        buttonbox = QHBoxLayout()
        buttonbox2 = QHBoxLayout()

        btn7 = QPushButton("mm/s: 10.1")
        btn7.setEnabled(False)
        buttonbox2.addWidget(btn7)
Then I want change button text to peak value:

    def time_signal(self):
        serial_read()
        self.ydata = time_data
        self.canvas.axes.cla()  
        self.canvas.axes.plot(self.ydata[0:500], 'b')
        self.canvas.draw()

        btn7.settext(str(max(g_data)))   #----------------------------------------------------------
     
Is there any way how to change button text? I tried self.btn7, etc. etc. but no success.
Thank you


RE: PyQt + Matplotlib - Axel_Erfurt - Apr-02-2022

QT uses camelCase, use setText and self

self.btn7 = QPushButton("mm/s: 10.1")

self.btn7.setText(str(max(g_data)))