如何将参数从PyQt5 QSpinBox传输到Matplotlib?

时间:2018-09-12 00:55:48

标签: python matplotlib pyqt5

原始代码来自: https://github.com/boxcontrol/matplotlibPyQt5/blob/master/matplotlibPyQt5.py

我对代码进行了一些更改,现在代码如下。

import sys
import random
import matplotlib
matplotlib.use("Qt5Agg")
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget, QSpinBox

from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure


class MyMplCanvas(FigureCanvas):
    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        # We want the axes cleared every time plot() is called
        # self.axes.hold(False)

        self.compute_initial_figure()

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                QSizePolicy.Expanding,
                QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        pass

class MyStaticMplCanvas(MyMplCanvas):
    """Simple canvas with a sine plot."""
    def compute_initial_figure(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2*pi*t)  # by clicking the QSpinBox, this line should be like s = sin(value_from_QSpinBox * pi * t)
        self.axes.plot(t, s)


class ApplicationWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle("application main window")

        self.file_menu = QMenu('&File', self)
        self.file_menu.addAction('&Quit', self.fileQuit,
                QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
        self.menuBar().addMenu(self.file_menu)

        self.main_widget = QWidget(self)

        self.sb = QSpinBox(self)
        self.sb.setMinimum(1)
        self.sb.setMaximum(10)
        self.sb.setSingleStep(1)
        self.sb.setValue(1)
        self.sb.resize(self.sb.sizeHint())

        l = QVBoxLayout(self.main_widget)
        sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100)
        l.addWidget(self.sb)
        l.addWidget(sc)

        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)

        self.statusBar().showMessage("All hail matplotlib!", 2000)

    def fileQuit(self):
        self.close()

    def closeEvent(self, ce):
        self.fileQuit()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    aw = ApplicationWindow()
    aw.setWindowTitle("PyQt5 Matplot Example")
    aw.show()
    app.exec_()

窗口中有一个QSpinBox和一个matplotlib图表。通过更改QSpinBox的值,我想更改matplotlib-diagram。

确切地说,以下行应从QSpinBox中获取参数,并且应相应地更新正弦图。

s = sin(2*pi*t)  # by clicking the QSpinBox, this line should be like s = sin(value_from_QSpinBox * pi * t)

我已经尝试了很多,但是都失败了。有人可以帮我吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

您必须使用valueChanged的{​​{1}}信号,为此,您必须修改绘图所执行的功能,如我所示:

QSpinBox