Python Matplotlib事件处理:何时发送Matplotlib事件,何时发送Qt事件
我正在尝试使用Pyside2将matplotlib嵌入到Qt接口中。我想用“imshow”来显示一个图像,我可以用滚轮放大并点击。要连接滚轮和鼠标,我使用:Python Matplotlib事件处理:何时发送Matplotlib事件,何时发送Qt事件,python,matplotlib,pyside2,Python,Matplotlib,Pyside2,我正在尝试使用Pyside2将matplotlib嵌入到Qt接口中。我想用“imshow”来显示一个图像,我可以用滚轮放大并点击。要连接滚轮和鼠标,我使用: self.fig.canvas.mpl_connect('button_press_event', self.mouseClick) self.fig.canvas.mpl_connect('scroll_event', self.wheelEvent) 如果对于我使用的事件处理程序: def wheelEvent(self, event
self.fig.canvas.mpl_connect('button_press_event', self.mouseClick)
self.fig.canvas.mpl_connect('scroll_event', self.wheelEvent)
如果对于我使用的事件处理程序:
def wheelEvent(self, event):
print(event)
def mouseClick(self, event):
print(event)
当我运行此命令,然后滚动并单击时,我得到的输出是:
button_press_event: xy=(94, 199) xydata=(99.99197230814553, 134.93602887023127) button=1 dblclick=False inaxes=AxesSubplot(0.125,0.290018;0.775x0.409964)
<PySide2.QtGui.QWheelEvent object at 0x7f92e3359a50>
button_press_事件:xy=(94199)xydata=(99.99197230814553134.93602887023127)button=1 dblclick=False inaxes=axesssubplot(0.125,0.290018;0.775x0.409964)
所以我的问题是,为什么在一种情况下我得到一个PySide事件,而在另一种情况下我得到一个matplotlib按钮\u press\u事件
?我所期望的是得到一个scroll\u事件
非常感谢 说明:
wheelEvent是QWidget类的一个方法,当窗口通过QWheelEvent具有焦点时,每次它与控制盘交互时都会收到通知,因此从它继承的类也将具有它,在这种情况下,它们会覆盖该方法,并且您也将其连接到scroll_事件,因此打印QWheelEvent(对于Linux上的PySide2 5.14,我交替接收QWheelEvent和scroll_事件)
解决方案:
不要使用wheelEvent方法,而是使用另一个名称的方法:
self.fig.canvas.mpl\u connect('button\u press\u event',self.mouseClick)
self.fig.canvas.mpl\u connect('scroll\u event',self.mouseweel)
def鼠标滚轮(自身,事件):
打印(事件)
def鼠标单击(自身、事件):
打印(事件)
谢谢!我花了6年以上的时间才弄明白。。。
self.fig.canvas.mpl_connect('button_press_event', self.mouseClick)
self.fig.canvas.mpl_connect('scroll_event', self.mouseWheel)
def mouseWheel(self, event):
print(event)
def mouseClick(self, event):
print(event)