python – Pyqtgraph:在哪里找到关键字母的信号?

以下示例显示如何将任意
python callable连接到GraphicsWindow中的鼠标运动事件.您如何为重点新闻事件做同样的事情?

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg

app = pg.mkQApp()
win = pg.GraphicsWindow()
pl = win.addPlot()
pl.plot([x*x for x in range(-10,11)])

def mouseMoved(evt):
    print("Mouse moved event: {}".format(evt))

pl.scene().sigMouseMoved.connect(mouseMoved)

def keyPressed(evt):
    print("Key pressed")

# The scene doesn't have an equivalent signal for key presses
# pl.scene().sigKeyPressed.connect(keyPress)

app.exec_()

最佳答案 尽管如此,自从提出这个问题以来已经有一段时间了,

我仍然希望我的回答有所帮助.

解决方案是从pyqtgraph.GraphicsWindow派生一个类,然后定义一个按键信号.

from pyqtgraph.Qt import QtCore
import pyqtgraph as pg

class KeyPressWindow(pg.GraphicsWindow):
    sigKeyPress = QtCore.pyqtSignal(object)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def keyPressEvent(self, ev):
        self.scene().keyPressEvent(ev)
        self.sigKeyPress.emit(ev)


def keyPressed(evt):
    print("Key pressed")


app = pg.mkQApp()
win = KeyPressWindow()
win.sigKeyPress.connect(keyPressed)
pl = win.addPlot()
pl.plot([x*x for x in range(-10,11)])


app.exec_()
点赞