paulbrodersen / netgraph

Publication-quality network visualisations in python
GNU General Public License v3.0
660 stars 39 forks source link

Embedding NetGraph EditableGraph in PyQt5 #85

Closed Franck-Delaplace closed 4 months ago

Franck-Delaplace commented 5 months ago

I try to embed the EditableGraph function into a PyQt5 application. I developed the following program for testing, based on solutions found online.

from PyQt5 import QtWidgets, QtCore
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from netgraph import EditableGraph, InteractiveGraph
class MainWindow(QtWidgets.QMainWindow):
     def __init__(self):
        super(MainWindow, self).__init__()
        self.canvas = FigureCanvas(Figure())
        self.canvas.ax = self.canvas.figure.add_subplot(111)
        self.editgraph = EditableGraph([(0, 1), (1,2), (2,0)], 
                                       ax=self.canvas.ax)

        # Enable key_press_event events:
        self.canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
        self.canvas.setFocus()

        widget = QtWidgets.QWidget()
        self.setCentralWidget(widget)
        layout = QtWidgets.QVBoxLayout(widget)
        layout.addWidget(self.canvas)

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    w = MainWindow()
    w.show()
    app.exec_()

However I have the following error:

Traceback (most recent call last): File "C:... \testit.py", line 26, in w = MainWindow() ^^^^^^^^^^^^ File "C:... \testit.py", line 13, in init self.graph = EditableGraph([(0, 1), (1,2), (2,0)], ax=self.canvas.ax) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C: ... \netgraph_interactive_graph_classes.py", line 1971, in init self.fig.canvas.manager.key_press = key_press_handler ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'key_press'

When I exchange EditableGraph by InteractiveGraph, it works fine. I don't know where the problem originated from, and I couldn't find a solution on the Web. Can you please help me? This embedding is crucial for my application. Thank you.

Franck-Delaplace commented 5 months ago

Finally, I found a solution which is described here: https://stackoverflow.com/questions/78052072/embedding-netgraph-editablegraph-in-pyqt5

paulbrodersen commented 5 months ago

Is your issue resolved then?

paulbrodersen commented 4 months ago

Marking this issue as resolved. Feel free to re-open if it isn't.