Python pyqtgraph获取节点文本并在鼠标单击时更改颜色
我从pyqtgraph github repo中找到了一个示例 我想用Python pyqtgraph获取节点文本并在鼠标单击时更改颜色,python,pyqt,pyqtgraph,Python,Pyqt,Pyqtgraph,我从pyqtgraph github repo中找到了一个示例 我想用pyqtgraph创建一个具有以下功能的交互式(网络)图: 当用户单击某个节点时,他/她将获得该节点的“文本”信息 那个节点 单击节点时,该节点的颜色以及 其所有邻居(边和节点)的颜色将更改(例如,变为黄色) 允许多选 代码: # -*- coding: utf-8 -*- """ Simple example of subclassing GraphItem. """ import initExample ## Add
pyqtgraph
创建一个具有以下功能的交互式(网络)图:
- 当用户单击某个节点时,他/她将获得该节点的“文本”信息 那个节点李>
- 单击节点时,该节点的颜色以及 其所有邻居(边和节点)的颜色将更改(例如,变为黄色)
- 允许多选
# -*- coding: utf-8 -*-
"""
Simple example of subclassing GraphItem.
"""
import initExample ## Add path to library (just for examples; you do not need this)
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
# Enable antialiasing for prettier plots
pg.setConfigOptions(antialias=True)
w = pg.GraphicsWindow()
w.setWindowTitle('pyqtgraph example: CustomGraphItem')
v = w.addViewBox()
v.setAspectLocked()
class Graph(pg.GraphItem):
def __init__(self):
self.dragPoint = None
self.dragOffset = None
self.textItems = []
pg.GraphItem.__init__(self)
self.scatter.sigClicked.connect(self.clicked)
def setData(self, **kwds):
self.text = kwds.pop('text', [])
self.data = kwds
if 'pos' in self.data:
npts = self.data['pos'].shape[0]
self.data['data'] = np.empty(npts, dtype=[('index', int)])
self.data['data']['index'] = np.arange(npts)
self.setTexts(self.text)
self.updateGraph()
def setTexts(self, text):
for i in self.textItems:
i.scene().removeItem(i)
self.textItems = []
for t in text:
item = pg.TextItem(t)
self.textItems.append(item)
item.setParentItem(self)
def updateGraph(self):
pg.GraphItem.setData(self, **self.data)
for i,item in enumerate(self.textItems):
item.setPos(*self.data['pos'][i])
def mouseDragEvent(self, ev):
if ev.button() != QtCore.Qt.LeftButton:
ev.ignore()
return
if ev.isStart():
# We are already one step into the drag.
# Find the point(s) at the mouse cursor when the button was first
# pressed:
pos = ev.buttonDownPos()
pts = self.scatter.pointsAt(pos)
if len(pts) == 0:
ev.ignore()
return
self.dragPoint = pts[0]
ind = pts[0].data()[0]
self.dragOffset = self.data['pos'][ind] - pos
elif ev.isFinish():
self.dragPoint = None
return
else:
if self.dragPoint is None:
ev.ignore()
return
ind = self.dragPoint.data()[0]
self.data['pos'][ind] = ev.pos() + self.dragOffset
self.updateGraph()
ev.accept()
def clicked(self, pts):
print("clicked: %s" % pts)
g = Graph()
v.addItem(g)
## Define positions of nodes
pos = np.array([
[0,0],
[10,0],
[0,10],
[10,10],
[5,5],
[15,5]
], dtype=float)
## Define the set of connections in the graph
adj = np.array([
[0,1],
[1,3],
[3,2],
[2,0],
[1,5],
[3,5],
])
## Define the symbol to use for each node (this is optional)
symbols = ['o','o','o','o','t','+']
## Define the line style for each connection (this is optional)
lines = np.array([
(255,0,0,255,1),
(255,0,255,255,2),
(255,0,255,255,3),
(255,255,0,255,2),
(255,0,0,255,1),
(255,255,255,255,4),
], dtype=[('red',np.ubyte),('green',np.ubyte),('blue',np.ubyte),('alpha',np.ubyte),('width',float)])
## Define text to show next to each symbol
texts = ["Point %d" % i for i in range(6)]
## Update the graph
g.setData(pos=pos, adj=adj, pen=lines, size=1, symbol=symbols, pxMode=False, text=texts)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
单击节点时,上述代码返回以下信息:
这实际上是整个图形项(因为在整个图形上调用了单击信号)
现在:如何在每个节点上调用MouseClicking功能,获取该节点的文本并在单击事件中更改其(及其邻居)的颜色
显示在另一个绘图中更改单击曲线的颜色。在这里,在每条曲线上都会调用单击信号。我试图以此为起点,在下面提到的第一个代码示例中实现类似的功能,但老实说,我甚至不知道如何访问pyqtgraph对象中的单个节点(因为[我可能错了],节点仅通过它们的位置定义)
一如既往,我们将非常感谢您的帮助
编辑:多亏了kesumu的回答,我能够获得点击节点的文本内容,如下所示:
def clicked(self, scatter, pts):
data_list = scatter.data.tolist()
mypoint = [tup for tup in data_list if pts[0] in tup][0]
mypoint_index = data_list.index(mypoint)
mypoint_text = self.text[mypoint_index]
def clicked(self, scatter, pts):
print(scatter)
print(pts[0])
print("clicked: %s" % pts)
编辑II:
同样的问题可以找到一个更详细的例子。问题在于您单击的
函数是错误的
应该是这样的:
def clicked(self, scatter, pts):
data_list = scatter.data.tolist()
mypoint = [tup for tup in data_list if pts[0] in tup][0]
mypoint_index = data_list.index(mypoint)
mypoint_text = self.text[mypoint_index]
def clicked(self, scatter, pts):
print(scatter)
print(pts[0])
print("clicked: %s" % pts)
因为sigClicked
有两个参数:self,points。这是它的定义:
sigClicked = QtCore.Signal(object, object) ## self, points
修复后,您可以获得如下点击点:
def clicked(self, scatter, pts):
data_list = scatter.data.tolist()
mypoint = [tup for tup in data_list if pts[0] in tup][0]
mypoint_index = data_list.index(mypoint)
mypoint_text = self.text[mypoint_index]
def clicked(self, scatter, pts):
print(scatter)
print(pts[0])
print("clicked: %s" % pts)
希望这对您有所帮助。问题是您单击的函数错误
应该是这样的:
def clicked(self, scatter, pts):
data_list = scatter.data.tolist()
mypoint = [tup for tup in data_list if pts[0] in tup][0]
mypoint_index = data_list.index(mypoint)
mypoint_text = self.text[mypoint_index]
def clicked(self, scatter, pts):
print(scatter)
print(pts[0])
print("clicked: %s" % pts)
因为sigClicked
有两个参数:self,points。这是它的定义:
sigClicked = QtCore.Signal(object, object) ## self, points
修复后,您可以获得如下点击点:
def clicked(self, scatter, pts):
data_list = scatter.data.tolist()
mypoint = [tup for tup in data_list if pts[0] in tup][0]
mypoint_index = data_list.index(mypoint)
mypoint_text = self.text[mypoint_index]
def clicked(self, scatter, pts):
print(scatter)
print(pts[0])
print("clicked: %s" % pts)
希望这对您有所帮助。kesumu,非常感谢您的帮助,它确实非常有用!我现在能够获得点击的节点以及它们的文本(将编辑如何获得文本)。但是现在我被困在如何改变点击节点的颜色以及它的邻居。。。我将把它放在另一个问题中,因为这个问题的主要关注点实际上是获取单击的节点及其文本内容。再次:谢谢!克苏木,非常感谢你的帮助,它确实非常有用!我现在能够获得点击的节点以及它们的文本(将编辑如何获得文本)。但是现在我被困在如何改变点击节点的颜色以及它的邻居。。。我将把它放在另一个问题中,因为这个问题的主要关注点实际上是获取单击的节点及其文本内容。再次:谢谢!