gpt4 book ai didi

python - Pyqtgraph:在哪里可以找到按键信号?

转载 作者:太空狗 更新时间:2023-10-30 01:12:46 25 4
gpt4 key购买 nike

以下示例展示了如何将任意 python 可调用对象连接到 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_()

关于python - Pyqtgraph:在哪里可以找到按键信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40423999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com