无法在PyQtGraph上获取mouseClickEvent触发器

问题描述 投票:0回答:1

我有一个渲染PyQtGraph的脚本。图中的节点应支持鼠标单击事件(尤其是左键单击)并获取鼠标单击的位置。我试过让它工作但我不确定我做错了什么。以下是我的代码中的代码段。

class Graph(pg.GraphItem):
    def __init__(self):
        pg.GraphItem.__init__(self)
        self.scatter.sigClicked.connect(self.onclick(pg.GraphItem)) <<<<<<<<<<

    def onclick(self, item):
        items = item.pos(self)
        print("Plots:", [x for x in items if isinstance(x, pg.GraphItem)])

pg.setConfigOption('background', 'k')
pg.setConfigOption('foreground', 'w')
pg.setConfigOptions(antialias=True)

w = pg.GraphicsWindow()  # creating an instance of the PyQt GraphicsWindow
w.setWindowTitle('H2 tree for Emails')  # set the title of the graphic window
v = w.addViewBox()  # add a view box to the graphic window
v.setAspectLocked()
g = Graph()  # create an instance of the class Graph
v.addItem(g)  # add the created graph instance to the view box

g.setData(pos=positions, adj=adj, size=0.01, pxMode=False, text=text)  # set the node in the graphic window

它在标有箭头的语句处抛出错误,错误“TypeError:参数1具有意外类型'NoneType'”。

有人可以帮我这个。我不打算在图形渲染代码中进行任何更改。

python mouseclick-event pyqtgraph
1个回答
1
投票

问题是由于您将sigClicked信号错误地连接到连接函数,您必须传递函数的名称,您不应该传递一个项目:

class Graph(pg.GraphItem):
    def __init__(self):
        pg.GraphItem.__init__(self)
        self.scatter.sigClicked.connect(self.onclick) 

    def onclick(self, plot, points):
        for point in points:
            print(point.pos())
© www.soinside.com 2019 - 2024. All rights reserved.