Adding a PyQT5 gui over a vispy simulation

65 Views Asked by At

I currently have two files, one using PyQt5 to create a simple GUI layout of data pulled from a JSON file, and another file using vispy to open a simulation. How can I get the GUI to overlay the simulation?

I currently have tried importing the GUI into the simulation file and initializing and running it there, however, that did not work. I also tried creating an instance of the simulation inside of the GUI file and that did not work either.

1

There are 1 best solutions below

1
harriet On

You need to embed your vispy canvas into your pyqt5 gui. Creating an instance of the simulation inside the gui file won't work without subsequently adding the canvas wrapper to the main layout/frame/widget. Running the gui from the simulation file won't work. You need to specify pyqt5 as the backend: app = use_app("pyqt5") Here is a bare-bones example:

from PyQt5 import QtWidgets
from vispy.scene import SceneCanvas, visuals
from vispy.app import use_app

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        central_widget = QtWidgets.QWidget()
        main_layout = QtWidgets.QHBoxLayout()
        # initialize control class & add to widget
        self._controls = Controls()
        main_layout.addWidget(self._controls)
        # initialize vispy canvas & add to widget
        self._canvas_wrapper = CanvasWrapper()
        main_layout.addWidget(self._canvas_wrapper.canvas.native)
        central_widget.setLayout(main_layout)
        self.setCentralWidget(central_widget)

class Controls(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        layout = QtWidgets.QVBoxLayout()
        self.test_label = QtWidgets.QLabel("Label:")
        layout.addWidget(self.test_label)
        self.test_options = QtWidgets.QComboBox()
        self.test_options.addItems(["option1", "option2", "option3"])
        layout.addWidget(self.test_options)
        layout.addStretch(1)
        self.setLayout(layout)

class CanvasWrapper:
    def __init__(self):
        self.canvas = SceneCanvas(size=(800, 600))
        self.grid = self.canvas.central_widget.add_grid()
        self.grid.add_view(0, 0, bgcolor='cyan')

if __name__ == "__main__":
    app = use_app("pyqt5")
    app.create()
    win = MainWindow()
    win.show()

which will run:

enter image description here