带有选项卡的PYQT应用程序中的可点击NetGraph
我有一个PYQT应用程序,该应用程序具有一个可以打开任意数量的选项卡的选项卡窗口小部件。每个选项卡嵌入一个显示图形的matplotlib帆布。
最近,我试图从netgraph
库中实现InteractiveGraph
,尽管其他stackoverflow相似的主题。我不知道,也许这来自额外的标签。
我观察到的是我无法单击图节点。图形已正确显示。
下面是我的代码的快速示例(选项卡具有用于测试的静态图值,每个添加的选项卡添加了一个嵌入式图),以及我如何尝试实现类似主题的建议解决方案。我不确定使用mpl_connect
的必要性,因此我尝试过和没有它,但没有任何更改。
import sys
from PyQt5 import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.pyplot import Figure
import networkx as nx
import numpy as np
from netgraph import InteractiveGraph
class data_tab(QtWidgets.QWidget):
def __init__(self, parent, title):
QtWidgets.QWidget.__init__(self, parent)
self.data_tab_glayout = QtWidgets.QGridLayout(self)
self.canvas = FigureCanvas(Figure(figsize=(5, 3)))
self.canvas.setParent(parent)
self.canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
self.canvas.setFocus()
self.canvas_vlayout = QtWidgets.QVBoxLayout(self.canvas)
self.data_tab_glayout.addWidget(self.canvas, 0, 0, 2, 1)
self.canvas.mpl_connect('key_press_event', self.on_key_press)
self.axe = self.canvas.figure.add_subplot(111)
self.canvas.figure.subplots_adjust(left=0.025, top=0.965, bottom=0.040, right=0.975)
# add the tab to the parent
parent.addTab(self, "")
# set text name
parent.setTabText(parent.indexOf(self), title)
def on_key_press(self, event):
print("you press", event.key)
class MyApp(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.showMaximized()
self.centralwidget = QtWidgets.QWidget(self)
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.core_tab = QtWidgets.QTabWidget(self.centralwidget)
self.verticalLayout.addWidget(self.core_tab)
self.add_tab_btn = QtWidgets.QPushButton(self.centralwidget)
self.verticalLayout.addWidget(self.add_tab_btn)
self.refresh_tab_btn = QtWidgets.QPushButton(self.centralwidget)
self.verticalLayout.addWidget(self.refresh_tab_btn)
self.setCentralWidget(self.centralwidget)
self.add_tab_btn.setText("Add Tab")
self.refresh_tab_btn.setText("Refresh Tabs")
self.core_tab.setEnabled(True)
self.core_tab.setTabShape(QtWidgets.QTabWidget.Rounded)
self.core_tab.setElideMode(QtCore.Qt.ElideNone)
self.core_tab.setDocumentMode(False)
self.core_tab.setTabsClosable(True)
self.core_tab.setMovable(True)
self.core_tab.setTabBarAutoHide(False)
self.tab_counter = 0
self.random_tabs = [("a", ["b", "c"]),
("d", ["e", "f", "g"]),
("h", ["i", "j", "k", "l"]),
("m", ["n"]),
("o", ["p", "q"]),
("r", ["s", "t", "u", "v", "w", "x", "y", "z"])]
self.add_tab_btn.clicked.connect(self.openRandomTab)
self.refresh_tab_btn.clicked.connect(self.refreshAllTabs)
def openRandomTab(self):
tab = data_tab(self.core_tab, "test " + str(self.tab_counter))
self._drawDataGraph(self.tab_counter % len(self.random_tabs), tab)
self.tab_counter += 1
self.core_tab.setCurrentIndex(self.core_tab.indexOf(tab))
def _drawDataGraph(self, tabNb, dataWidget):
dataWidget.axe.cla()
# 1. draw graph
producer = self.random_tabs[tabNb][0]
consumers = self.random_tabs[tabNb][1]
color_map = []
DG = nx.DiGraph()
for i, cons in enumerate(consumers):
DG.add_edge(producer, cons, label=f"edge-{i}")
node_color = dict()
for node in DG:
if node in producer:
node_color[node] = "#DCE46F"
else:
node_color[node] = "#6FA2E4"
pos = nx.shell_layout(DG)
pos[producer] = pos[producer] + np.array([0.2, 0])
labels = nx.get_edge_attributes(DG, 'label')
graph_instance = InteractiveGraph(DG, node_layout=pos, edge_layout='curved', origin=(-1, -1), scale=(2, 2),
node_color=node_color, node_size=8.,
node_labels=True, node_label_fontdict=dict(size=10),
edge_labels=labels, edge_label_fontdict=dict(size=10), ax=dataWidget.axe
)
dataWidget.canvas.draw()
def refreshAllTabs(self):
# loop through all pages and associated to get
for tab_index in range(self.core_tab.count()):
data_tab_widget = self.core_tab.widget(tab_index)
# draw graph
self._drawDataGraph(tab_index % len(self.random_tabs), data_tab_widget)
sys.argv = ['']
app = QtWidgets.QApplication(sys.argv)
main_app = MyApp()
main_app.show()
app.exec_()
I have a PyQt application which has a tab widget that can open any number of tabs. Each tab embeds a matplotlib canvas displaying graphs.
Lately, I have tried to implement InteractiveGraph
from netgraph
library, with little success despite the help of other stackoverflow similar topic. Maybe it comes from the additional presence of tabs, I don't know.
What I observe is that I can't manage to click on the graph nodes. Graph are properly displayed though.
Below is a quick example of my code (tabs have static graph values for testing, each added tab add an embedded graph), and how I tried to implement proposed solution of similar topic. I was not sure about necessity of using mpl_connect
, so I tried with and without it and it didn't change anything.
import sys
from PyQt5 import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.pyplot import Figure
import networkx as nx
import numpy as np
from netgraph import InteractiveGraph
class data_tab(QtWidgets.QWidget):
def __init__(self, parent, title):
QtWidgets.QWidget.__init__(self, parent)
self.data_tab_glayout = QtWidgets.QGridLayout(self)
self.canvas = FigureCanvas(Figure(figsize=(5, 3)))
self.canvas.setParent(parent)
self.canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
self.canvas.setFocus()
self.canvas_vlayout = QtWidgets.QVBoxLayout(self.canvas)
self.data_tab_glayout.addWidget(self.canvas, 0, 0, 2, 1)
self.canvas.mpl_connect('key_press_event', self.on_key_press)
self.axe = self.canvas.figure.add_subplot(111)
self.canvas.figure.subplots_adjust(left=0.025, top=0.965, bottom=0.040, right=0.975)
# add the tab to the parent
parent.addTab(self, "")
# set text name
parent.setTabText(parent.indexOf(self), title)
def on_key_press(self, event):
print("you press", event.key)
class MyApp(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.showMaximized()
self.centralwidget = QtWidgets.QWidget(self)
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.core_tab = QtWidgets.QTabWidget(self.centralwidget)
self.verticalLayout.addWidget(self.core_tab)
self.add_tab_btn = QtWidgets.QPushButton(self.centralwidget)
self.verticalLayout.addWidget(self.add_tab_btn)
self.refresh_tab_btn = QtWidgets.QPushButton(self.centralwidget)
self.verticalLayout.addWidget(self.refresh_tab_btn)
self.setCentralWidget(self.centralwidget)
self.add_tab_btn.setText("Add Tab")
self.refresh_tab_btn.setText("Refresh Tabs")
self.core_tab.setEnabled(True)
self.core_tab.setTabShape(QtWidgets.QTabWidget.Rounded)
self.core_tab.setElideMode(QtCore.Qt.ElideNone)
self.core_tab.setDocumentMode(False)
self.core_tab.setTabsClosable(True)
self.core_tab.setMovable(True)
self.core_tab.setTabBarAutoHide(False)
self.tab_counter = 0
self.random_tabs = [("a", ["b", "c"]),
("d", ["e", "f", "g"]),
("h", ["i", "j", "k", "l"]),
("m", ["n"]),
("o", ["p", "q"]),
("r", ["s", "t", "u", "v", "w", "x", "y", "z"])]
self.add_tab_btn.clicked.connect(self.openRandomTab)
self.refresh_tab_btn.clicked.connect(self.refreshAllTabs)
def openRandomTab(self):
tab = data_tab(self.core_tab, "test " + str(self.tab_counter))
self._drawDataGraph(self.tab_counter % len(self.random_tabs), tab)
self.tab_counter += 1
self.core_tab.setCurrentIndex(self.core_tab.indexOf(tab))
def _drawDataGraph(self, tabNb, dataWidget):
dataWidget.axe.cla()
# 1. draw graph
producer = self.random_tabs[tabNb][0]
consumers = self.random_tabs[tabNb][1]
color_map = []
DG = nx.DiGraph()
for i, cons in enumerate(consumers):
DG.add_edge(producer, cons, label=f"edge-{i}")
node_color = dict()
for node in DG:
if node in producer:
node_color[node] = "#DCE46F"
else:
node_color[node] = "#6FA2E4"
pos = nx.shell_layout(DG)
pos[producer] = pos[producer] + np.array([0.2, 0])
labels = nx.get_edge_attributes(DG, 'label')
graph_instance = InteractiveGraph(DG, node_layout=pos, edge_layout='curved', origin=(-1, -1), scale=(2, 2),
node_color=node_color, node_size=8.,
node_labels=True, node_label_fontdict=dict(size=10),
edge_labels=labels, edge_label_fontdict=dict(size=10), ax=dataWidget.axe
)
dataWidget.canvas.draw()
def refreshAllTabs(self):
# loop through all pages and associated to get
for tab_index in range(self.core_tab.count()):
data_tab_widget = self.core_tab.widget(tab_index)
# draw graph
self._drawDataGraph(tab_index % len(self.random_tabs), data_tab_widget)
sys.argv = ['']
app = QtWidgets.QApplication(sys.argv)
main_app = MyApp()
main_app.show()
app.exec_()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您单击该图,我认为您需要在图上连接
mpl_connect
,而不是在画布上,然后与
'button_press_event'
连接 单击的节点可以在
dg
和graph_instance
中放在data_tab
类中,然后在回调函数中使用
它们喜欢
If you what click on the figure, I think you need to connect
mpl_connect
on the figure, not on the canvasthen make the connection with the
'button_press_event'
If you want get the node that was clicked on, you can put
DG
andgraph_instance
in thedata_tab
classthen use them in the callback function
the function could be something like
好的,在YMMX答案之后,我发现InteractiveGraph的实例不需要本地化。
因此,这是在我的情况下似乎正如我想要的那样的解决方案:
OK, following ymmx answer, I figured out that the instanciation of InteractiveGraph needs to not be local.
So here's the solution which seems to behave as wanted in my case: