1. <small id='PWYEN'></small><noframes id='PWYEN'>

        <bdo id='PWYEN'></bdo><ul id='PWYEN'></ul>
      <tfoot id='PWYEN'></tfoot>
    2. <i id='PWYEN'><tr id='PWYEN'><dt id='PWYEN'><q id='PWYEN'><span id='PWYEN'><b id='PWYEN'><form id='PWYEN'><ins id='PWYEN'></ins><ul id='PWYEN'></ul><sub id='PWYEN'></sub></form><legend id='PWYEN'></legend><bdo id='PWYEN'><pre id='PWYEN'><center id='PWYEN'></center></pre></bdo></b><th id='PWYEN'></th></span></q></dt></tr></i><div id='PWYEN'><tfoot id='PWYEN'></tfoot><dl id='PWYEN'><fieldset id='PWYEN'></fieldset></dl></div>

      1. <legend id='PWYEN'><style id='PWYEN'><dir id='PWYEN'><q id='PWYEN'></q></dir></style></legend>

        如何在 QWidget 中的 matplot 画布上跟踪鼠标?

        时间:2023-08-05
        1. <legend id='OVx5T'><style id='OVx5T'><dir id='OVx5T'><q id='OVx5T'></q></dir></style></legend>
          • <i id='OVx5T'><tr id='OVx5T'><dt id='OVx5T'><q id='OVx5T'><span id='OVx5T'><b id='OVx5T'><form id='OVx5T'><ins id='OVx5T'></ins><ul id='OVx5T'></ul><sub id='OVx5T'></sub></form><legend id='OVx5T'></legend><bdo id='OVx5T'><pre id='OVx5T'><center id='OVx5T'></center></pre></bdo></b><th id='OVx5T'></th></span></q></dt></tr></i><div id='OVx5T'><tfoot id='OVx5T'></tfoot><dl id='OVx5T'><fieldset id='OVx5T'></fieldset></dl></div>
              <tbody id='OVx5T'></tbody>

              <bdo id='OVx5T'></bdo><ul id='OVx5T'></ul>

                • <tfoot id='OVx5T'></tfoot>

                  <small id='OVx5T'></small><noframes id='OVx5T'>

                  本文介绍了如何在 QWidget 中的 matplot 画布上跟踪鼠标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想实时跟踪鼠标在 matplot 画布上的位置.

                  I want to track the mouse's position over a matplot's canvas in real-time.

                  现在,我构建了一个继承 Qwidget(像容器一样)的 MplWidget,然后在它上面构建了一个画布来显示绘图.但是,问题是我只能在除画布区域之外的填充区域中跟踪鼠标的位置.

                  For now, I built a MplWidget that inherits the Qwidget (act like a container), then built a canvas over it to show the plot. However, the problem is that I can only track the mouse's position in the padding area except for the canvas area.

                  由于我的画布继承了不是 QWidget 的 matplotlib.figure,因此它没有 setMouseTracking() 属性.这样,如何解决这个问题?

                  Since my canvas inherits the matplotlib.figure that is not a QWidget, thus it doesn't have the setMouseTracking() attribute. In this way, how to resolve this issue?

                  我发现了一个非常有用的链接如何实时返回鼠标坐标?.然而,它也面临同样的问题.当鼠标在标签(文本区域)上时,跟踪功能似乎被中断了.

                  I found a quite useful link How to return mouse coordinates in realtime?. However, it also suffers the same issue. When the mouse is over the label (text area), the tracking function seems to be interrupted.

                  我的这个类的代码如下所示:

                  my code for this class shown here:

                  from PyQt5.QtWidgets import *
                  
                  from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
                  
                  from matplotlib.figure import Figure
                  
                  
                  class MplWidget(QWidget):
                  
                      def __init__(self, parent=None):
                          # QWidget.__init__(self, parent)
                          super(QWidget, self).__init__(parent)
                  
                          self.canvas = FigureCanvas(Figure())
                  
                          vertical_layout = QVBoxLayout()
                          vertical_layout.addWidget(self.canvas)
                  
                          self.canvas.axes = self.canvas.figure.add_subplot(111)
                          self.setLayout(vertical_layout)
                  
                          self.setMouseTracking(True)
                  
                      def mouseMoveEvent(self, e):
                          text = "x: {0},  y: {1}".format(e.x(), e.y())
                          print(text)
                          super(MplWidget, self).mouseMoveEvent(e)
                  
                      def mousePressEvent(self, e):
                          print('click!')
                  

                  推荐答案

                  您已经注意到画布不是由 Qt 而是由 matplotlib 处理的,因此解决方案是使用该库提供的事件,如果您查看 文档 你看到有以下事件:

                  As you have noticed the canvas is not handled by Qt but by matplotlib so the solution is to use the events provided by that library, if you review the docs you see that there are the following events:

                  事件名称 类别和描述

                  'button_press_event'  MouseEvent - mouse button is pressed
                  'button_release_event'    MouseEvent - mouse button is released
                  'draw_event'  DrawEvent - canvas draw (but before screen update)
                  'key_press_event' KeyEvent - key is pressed
                  'key_release_event'   KeyEvent - key is released
                  'motion_notify_event' MouseEvent - mouse motion
                  'pick_event'  PickEvent - an object in the canvas is selected
                  'resize_event'    ResizeEvent - figure canvas is resized
                  'scroll_event'    MouseEvent - mouse scroll wheel is rolled
                  'figure_enter_event'  LocationEvent - mouse enters a new figure
                  'figure_leave_event'  LocationEvent - mouse leaves a figure
                  'axes_enter_event'    LocationEvent - mouse enters a new axes
                  'axes_leave_event'    LocationEvent - mouse leaves an axes
                  

                  在您的情况下,您应该使用事件:

                  In your case you should use the events:

                  • button_press_event
                  • button_release_event
                  • motion_notify_event

                  例子:

                  from PyQt5 import QtWidgets
                  
                  from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
                  from matplotlib.figure import Figure
                  
                  
                  class MplWidget(QtWidgets.QWidget):
                      def __init__(self, parent=None):
                          super(MplWidget, self).__init__(parent)
                          self.canvas = FigureCanvas(Figure())
                  
                          vertical_layout = QtWidgets.QVBoxLayout(self)
                          vertical_layout.addWidget(self.canvas)
                  
                          self.canvas.axes = self.canvas.figure.add_subplot(111)
                  
                          self.canvas.mpl_connect("button_press_event", self.on_press)
                          self.canvas.mpl_connect("button_release_event", self.on_release)
                          self.canvas.mpl_connect("motion_notify_event", self.on_move)
                  
                      def on_press(self, event):
                          print("press")
                          print("event.xdata", event.xdata)
                          print("event.ydata", event.ydata)
                          print("event.inaxes", event.inaxes)
                          print("x", event.x)
                          print("y", event.y)
                  
                      def on_release(self, event):
                          print("release:")
                          print("event.xdata", event.xdata)
                          print("event.ydata", event.ydata)
                          print("event.inaxes", event.inaxes)
                          print("x", event.x)
                          print("y", event.y)
                  
                      def on_move(self, event):
                          print("move")
                          print("event.xdata", event.xdata)
                          print("event.ydata", event.ydata)
                          print("event.inaxes", event.inaxes)
                          print("x", event.x)
                          print("y", event.y)
                  
                  
                  if __name__ == "__main__":
                      import sys
                  
                      app = QtWidgets.QApplication(sys.argv)
                      w = MplWidget()
                      w.show()
                      sys.exit(app.exec_())
                  

                  这篇关于如何在 QWidget 中的 matplot 画布上跟踪鼠标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:检测 Widget-window resized 信号中的调整大小 下一篇:我如何用 pafy 为进度条制作线程

                  相关文章

                  最新文章

                • <i id='8RZiB'><tr id='8RZiB'><dt id='8RZiB'><q id='8RZiB'><span id='8RZiB'><b id='8RZiB'><form id='8RZiB'><ins id='8RZiB'></ins><ul id='8RZiB'></ul><sub id='8RZiB'></sub></form><legend id='8RZiB'></legend><bdo id='8RZiB'><pre id='8RZiB'><center id='8RZiB'></center></pre></bdo></b><th id='8RZiB'></th></span></q></dt></tr></i><div id='8RZiB'><tfoot id='8RZiB'></tfoot><dl id='8RZiB'><fieldset id='8RZiB'></fieldset></dl></div>
                    <bdo id='8RZiB'></bdo><ul id='8RZiB'></ul>

                  <legend id='8RZiB'><style id='8RZiB'><dir id='8RZiB'><q id='8RZiB'></q></dir></style></legend>
                • <tfoot id='8RZiB'></tfoot>

                  <small id='8RZiB'></small><noframes id='8RZiB'>