Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在嵌入matplotlib画布的Qt4应用程序中连接鼠标事件_Python_Qt_Matplotlib_Pyqt4_Pyside - Fatal编程技术网

Python 在嵌入matplotlib画布的Qt4应用程序中连接鼠标事件

Python 在嵌入matplotlib画布的Qt4应用程序中连接鼠标事件,python,qt,matplotlib,pyqt4,pyside,Python,Qt,Matplotlib,Pyqt4,Pyside,我有一个在python 2.7中使用QT设计和显示的绘图。我想将以下两个侦听器连接到它: 按钮按下事件 动议通知事件 在过去的早些时候,我使用的是plain,我使用的方法如下: import matplotlib.pyplot as plt def mouseClick(event): pass def mouseMove(event): pass fig = plt.figure() ax = fig.add_subplot(111) ax.set_xlim((xmin,

我有一个在python 2.7中使用QT设计和显示的绘图。我想将以下两个侦听器连接到它:

  • 按钮按下事件
  • 动议通知事件
  • 在过去的早些时候,我使用的是plain,我使用的方法如下:

    import matplotlib.pyplot as plt
    
    def mouseClick(event):
        pass
    
    def mouseMove(event):
        pass
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_xlim((xmin, xmax))
    ax.set_ylim((ymin, ymax))
    fig.canvas.draw()
    
    plt.connect('button_press_event', mouseClick)
    plt.connect('motion_notify_event', mouseMove)
    plt.show()
    
    下面是我的完整QT代码:

    import sys
    from matplotlib.backends import qt_compat
    use_pyside = qt_compat.QT_API == qt_compat.QT_API_PYSIDE
    if use_pyside:
        from PySide import QtGui, QtCore
    else:
        from PyQt4 import QtGui, QtCore
    
    from numpy import arange, sin, pi
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.figure import Figure
    
    class MyMplCanvas(FigureCanvas):
        def __init__(self, parent=None, width=5, height=4, dpi=100):
            fig = Figure(figsize=(width, height), dpi=dpi)
            self.axes = fig.add_subplot(111)
    
            t = arange(0.0, 3.0, 0.01)
            s = sin(2*pi*t)
            self.axes.plot(t, s)
    
            FigureCanvas.__init__(self, fig)
            self.setParent(parent)
    
            FigureCanvas.setSizePolicy(self, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
            FigureCanvas.updateGeometry(self)
    
    class ApplicationWindow(QtGui.QMainWindow):
        def __init__(self):
            QtGui.QMainWindow.__init__(self)
            self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
            self.setWindowTitle("application main window")
    
            self.main_widget = QtGui.QWidget(self)
            l = QtGui.QVBoxLayout(self.main_widget)
            sc = MyMplCanvas(self.main_widget, width=5, height=4, dpi=100)
            l.addWidget(sc)
            self.main_widget.setFocus()
            self.setCentralWidget(self.main_widget)
    
    qApp = QtGui.QApplication(sys.argv)
    aw = ApplicationWindow()
    aw.setWindowTitle("hello")
    aw.show()
    sys.exit(qApp.exec_())
    

    我想在上面的代码中添加列表器。非常感谢。

    对于这种情况,您必须使用图中的mpl\u connect

    sc.mpl_connect('button_press_event', self.mouseClick)
    sc.mpl_connect('motion_notify_event', self.mouseMove)
    
    代码:


    它就像一个符咒。非常感谢你。我想知道你是怎么找到它的?再次感谢。首先看看matplotlib中如何定义连接,我找到了以下引用:。在这里你可以看到连接是通过画布建立的,我认为这个方法是父画布的,所以你的孩子们也必须拥有这个属性,在你的例子中,FigureCanvas是画布的孩子们。我明白了。我正在读你给我的推荐信。非常感谢你。
    class ApplicationWindow(QtGui.QMainWindow):
        def __init__(self):
            QtGui.QMainWindow.__init__(self)
            self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
            self.setWindowTitle("application main window")
    
            self.main_widget = QtGui.QWidget(self)
            l = QtGui.QVBoxLayout(self.main_widget)
            sc = MyMplCanvas(self.main_widget, width=5, height=4, dpi=100)
            l.addWidget(sc)
            self.main_widget.setFocus()
            self.setCentralWidget(self.main_widget)
    
            sc.mpl_connect('button_press_event', self.mouseClick)
            sc.mpl_connect('motion_notify_event', self.mouseMove)
    
    
        def mouseClick(self, event):
            print(event)
    
        def mouseMove(self, event):
            print(event)