是否可以将 Mayavi 图嵌入到 PyQt5 小部件中?

问题描述 投票:0回答:2

我有一个

PyQt5
应用程序,想要将
mayavi
曲面图嵌入到我的一个小部件中。有可能吗?

python qt pyqt pyqt5 mayavi
2个回答
1
投票

因为 当前 Mayavi 4.5 的先决条件是

Qt 版本 4.8 与 PyQt 4.11.4 或 PySide 1.2.2,

你不太可能将它嵌入到 PyQt5 中。


0
投票

仅供参考,现在这是可能的。这是一个示例:

from PyQt5 import QtWidgets, QtCore
import numpy as np
from traits.api import HasTraits, Instance, on_trait_change
from traitsui.api import View, Item
from mayavi.core.ui.api import MayaviScene, MlabSceneModel, SceneEditor
import sys
import os
os.environ['ETS_TOOLKIT'] = 'qt4'


class Visualization(HasTraits):
    scene = Instance(MlabSceneModel, ())

    @on_trait_change('scene.activated')
    def update_plot(self):
        x, y = np.mgrid[-5:5:100j, -5:5:100j]
        z = np.random.random(x.shape) * 10
        # Creating the surface plot
        self.scene.mlab.surf(x, y, z, colormap='cool')
        self.scene.background = (1, 1, 1)

    view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
                     height=250, width=300, show_label=False),
                resizable=True)


class MayaviQWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        layout = QtWidgets.QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        self.visualization = Visualization()

        self.ui = self.visualization.edit_traits(parent=self,
                                                 kind='subpanel').control
        layout.addWidget(self.ui)
        self.ui.setParent(self)


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setGeometry(200, 200, 1100, 700)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        container = QtWidgets.QWidget()
        mayavi_widget = MayaviQWidget(container)
        self.gridLayout.addWidget(mayavi_widget, 0, 0, 0, 0)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Simulator"))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle('Fusion')
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

输出如下所示:

© www.soinside.com 2019 - 2024. All rights reserved.