我想知道何时单击图像矩阵外部(在附加的屏幕上,我想知道何时单击黑色空间)。
我的应用程序的目标是了解我何时单击图像框外部,以及了解我是否单击了图像周围黑色空间的左侧、顶部、右侧或底部。
import pyqtgraph as pg
import numpy as np
matrix = np.random.randint(1,10,(100, 100))
print(matrix)
app = pg.mkQApp()
win = pg.GraphicsLayoutWidget()
img = pg.ImageItem()
img.setImage(matrice)
view = win.addViewBox()
view.addItem(img)
def mouse_double_click(event):
print(event.pos())
print(event.scenePos())
print(event.screenPos())
img.scene().sigMouseClicked.connect(mouse_double_click)
win.show()
if __name__ == '__main__':
pg.exec()
img.mapFromScene(event.scenePos())
返回一个点,可以从该点检索x和y位置,因此函数可以更改为:
def mouse_double_click(event):
pos = img.mapFromScene(event.scenePos())
x, y = int(pos.x()), int(pos.y())
print(f"Click at x = {x}, y = {y}")
position = ""
if y >= 100:
position += "top"
elif y <= 0:
position += "bottom"
if x >= 100:
position += "right"
elif x <= 0:
position += "left"
if position:
print(f"The click was on the {position}")