无法在 PyQtGraph 上获取 mouseClickEvent 触发器
Unable to get mouseClickEvent trigger on PyQtGraph
我有一个呈现 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: argument 1 has unexpected type 'NoneType'" 的箭头的语句中抛出错误。
有人可以帮我解决这个问题吗?不过,我不打算对图形渲染代码进行任何更改。
问题是因为你错误地连接了 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())
我有一个呈现 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: argument 1 has unexpected type 'NoneType'" 的箭头的语句中抛出错误。
有人可以帮我解决这个问题吗?不过,我不打算对图形渲染代码进行任何更改。
问题是因为你错误地连接了 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())