python - pyqtgraph线的鼠标坐标

标签 python pyqt pyqt4 mouseevent pyqtgraph

每当我将鼠标移到图形顶部时,我都会尝试获取随机函数图的 (x,y) 值。我正在使用 pyqtgraph.SignalProxy 并将其连接到回调 mousedMoved。

我得到这个错误: NameError:未定义全局名称“mouseMoved”

代码如下:

import sys
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
import time
import random

class TestClass(QtGui.QMainWindow):
  #####################################################
  def __init__(self):
    super(TestClass, self).__init__()
    self.initUI()

  #####################################################
  # GUI construction
  def initUI(self):
    win = pg.GraphicsWindow(title="Mouse Point, x & y")

    # creates plot
    self.plot = pg.PlotWidget()
    self.plot.setLabel('left', "B", units='T')
    self.plot.setLabel('bottom', "t", units='s')
    self.plot.showGrid(x=1, y=1, alpha=None)
    self.setCentralWidget(win)
    self.setGeometry(600, 600, 600, 600)
    self.setWindowTitle('Mouse Point, x& y GUI')

    # Create some widgets to be placed inside
    btnRandon = QtGui.QPushButton('Random Function')


    # Create a grid layout to manage the widgets size and position
    layout = QtGui.QGridLayout()
    win.setLayout(layout)

    # Add widgets to the layout in their proper positions
    layout.addWidget(btnRandon, 0, 0) # button to show or hide the OldB
    layout.addWidget(self.plot, 1, 0)

    mypen = pg.mkPen('y', width=1)
    self.curve = self.plot.plot(pen=mypen)

    # Plot
    self.t_plot_max = 30
    self.fe = 10e3
    self.t = np.arange(-1 * self.t_plot_max, 0, 1.0 / self.fe)
    self.len_signal = len(self.t)
    self.signal = np.zeros(self.len_signal, dtype=np.double)

    # status bar
    self.statusBar()

    # clicked button evt
    btnRandon.clicked.connect(self.buttonRandomClicked)

    # show graph
    self.show()

  #####################################################
  def mouseMoved(evt):
    mousePoint = self.curve.vb.mapSceneToView(evt[0])
    label.setText("<span style='font-size: 14pt; color: white'> x = %0.2f, <span style='color: white'> y = %0.2f</span>" % (mousePoint.x(), mousePoint.y()))


  #####################################################
  def buttonRandomClicked(self):
    print ("Show/Hide OldB")
    self.signal = np.random.rand(20)
    self.curve.setData(self.signal)

#####################################################
  def update(self):
    proxy = pg.SignalProxy(self.curve.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)
    self.statusBar().showMessage('Update timer event')

# MAIN ##################################################
def main():
  app = QtGui.QApplication(sys.argv)
  ex = TestClass()
  timer = QtCore.QTimer()
  timer.timeout.connect(ex.update)
  timer.start(200)
  sys.exit(app.exec_())


if __name__ == '__main__':
  main()

我知道我做错了什么吗?

谢谢。

最佳答案

没有必要使用 QTimer 来执行这个任务并创建一个名为 update() 的方法,因为 QMainWindow 有一个类似的方法,可能会干扰正确的操作。

sigMouseMoved 信号会在您每次移动鼠标时发出,因此通常没有必要使用 SignalProxy

信号 sigMouseMoved 返回相对于 PlotWidget 的像素坐标,而不是绘图的坐标,因此必须使用 进行转换ViewBox 的 mapSceneToView 方法--> PlotItem --> PlotWidget.

最后,没有必要使用GraphicsWindow(),这会创建另一个窗口,只需一个QWidget 就足够了。

import sys
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
import random

class TestClass(QtGui.QMainWindow):
    #####################################################
    def __init__(self):
        super(TestClass, self).__init__()
        self.initUI()
    ####################################################
    # GUI construction
    def initUI(self):
        self.setWindowTitle("Mouse Point, x & y")
        win = QtGui.QWidget()
        # creates plot
        self.plot = pg.PlotWidget()
        self.plot.setLabel('left', "B", units='T')
        self.plot.setLabel('bottom', "t", units='s')
        self.plot.showGrid(x=1, y=1, alpha=None)
        self.setCentralWidget(win)
        self.setGeometry(600, 600, 600, 600)
        self.setWindowTitle('Mouse Point, x& y GUI')

        # Create some widgets to be placed inside
        btnRandon = QtGui.QPushButton('Random Function')
        # Create a grid layout to manage the widgets size and position
        layout = QtGui.QGridLayout(win)

        # Add widgets to the layout in their proper positions
        layout.addWidget(btnRandon, 0, 0) # button to show or hide the OldB
        layout.addWidget(self.plot, 1, 0)

        mypen = pg.mkPen('y', width=1)
        self.curve = self.plot.plot(x=[], y=[], pen=mypen)

        # Plot
        self.t_plot_max = 30
        self.fe = 10e3
        self.t = np.arange(-1 * self.t_plot_max, 0, 1.0 / self.fe)
        self.len_signal = len(self.t)
        self.signal = np.zeros(self.len_signal, dtype=np.double)

        btnRandon.clicked.connect(self.buttonRandomClicked)
        self.curve.scene().sigMouseMoved.connect(self.onMouseMoved)

    def onMouseMoved(self, point):
        p = self.plot.plotItem.vb.mapSceneToView(point)
        self.statusBar().showMessage("{}-{}".format(p.x(), p.y()))

    def buttonRandomClicked(self):
        print ("Show/Hide OldB")
        self.signal = np.random.rand(20)
        self.curve.setData(self.signal)


# MAIN ##################################################
def main():
    app = QtGui.QApplication(sys.argv)
    ex = TestClass()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

关于python - pyqtgraph线的鼠标坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50833107/

相关文章:

python - 如何使用 Qthread 通过 PyQt 更新 Matplotlib 图?

python - PyQt:十六进制的 QLineEdit 输入掩码

python - PyQt : Set text in a QLabel adding letter by letter

python - 如果卡住了 90 秒,如何从功能中返回?

python - 寻找 SyncML 协议(protocol)的纯 Python 库

python - 使用 Matplotlib 进行实时绘图。 X 轴被覆盖

Python:使用 TensorFlow 计算神经网络的准确性

python - 在 matplotlib 图中将鼠标限制在 Canvas 区域

python - 使用 PyQt4 的空白窗口

pyqt - 我可以将 Neumorphism 效果应用于 QWidget 吗?