m0_54212618 2023-04-16 15:56 采纳率: 63.6%
浏览 12
已结题

mayavi嵌入pyqt5后如何更新数据

mayavi 嵌入pyqt5后,不知道怎么更新数据。
打算用maltplotlib 或 pyqtgraph 画3d模型,但是数据量太大,所以决定用mayavi画,但是在网上找了一圈将mayavi嵌入pyqt5的方法,都是如下的方法:

# First, and before importing any Enthought packages, set the ETS_TOOLKIT
# environment variable to qt4, to tell Traits that we will use Qt.
import os
os.environ['ETS_TOOLKIT'] = 'qt4'
# By default, the PySide binding will be used. If you want the PyQt bindings
# to be used, you need to set the QT_API environment variable to 'pyqt'
#os.environ['QT_API'] = 'pyqt'

# To be able to use PySide or PyQt4 and not run in conflicts with traits,
# we need to import QtGui and QtCore from pyface.qt
from pyface.qt import QtGui, QtCore
# Alternatively, you can bypass this line, but you need to make sure that
# the following lines are executed before the import of PyQT:
#   import sip
#   sip.setapi('QString', 2)

from traits.api import HasTraits, Instance, on_trait_change
from traitsui.api import View, Item
from mayavi.core.ui.api import MayaviScene, MlabSceneModel, \
        SceneEditor


################################################################################
#The actual visualization
class Visualization(HasTraits):
    scene = Instance(MlabSceneModel, ())

    @on_trait_change('scene.activated')
    def update_plot(self):
        # This function is called when the view is opened. We don't
        # populate the scene when the view is not yet open, as some
        # VTK features require a GLContext.

        # We can do normal mlab calls on the embedded scene.
        self.scene.mlab.test_points3d()

    # the layout of the dialog screated
    view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
                     height=250, width=300, show_label=False),
                resizable=True # We need this to resize with the parent widget
                )


################################################################################
# The QWidget containing the visualization, this is pure PyQt4 code.
class MayaviQWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        layout = QtGui.QVBoxLayout(self)
        layout.setContentsMargins(0,0,0,0)
        layout.setSpacing(0)
        self.visualization = Visualization()

        # If you want to debug, beware that you need to remove the Qt
        # input hook.
        #QtCore.pyqtRemoveInputHook()
        #import pdb ; pdb.set_trace()
        #QtCore.pyqtRestoreInputHook()

        # The edit_traits call will generate the widget to embed.
        self.ui = self.visualization.edit_traits(parent=self,
                                                 kind='subpanel').control
        layout.addWidget(self.ui)
        self.ui.setParent(self)


if __name__ == "__main__":
    # Don't create a new QApplication, it would unhook the Events
    # set by Traits on the existing QApplication. Simply use the
    # '.instance()' method to retrieve the existing one.
    app = QtGui.QApplication.instance()
    container = QtGui.QWidget()
    container.setWindowTitle("Embedding Mayavi in a PyQt4 Application")
    # define a "complex" layout to test the behaviour
    layout = QtGui.QGridLayout(container)

    # put some stuff around mayavi
    label_list = []
    for i in range(3):
        for j in range(3):
            if (i==1) and (j==1):continue
            label = QtGui.QLabel(container)
            label.setText("Your QWidget at (%d, %d)" % (i,j))
            label.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)
            layout.addWidget(label, i, j)
            label_list.append(label)
    mayavi_widget = MayaviQWidget(container)

    layout.addWidget(mayavi_widget, 1, 1)
    container.show()
    window = QtGui.QMainWindow()
    window.setCentralWidget(container)
    window.show()

    # Start the main event loop.
    app.exec_()

这样嵌入后,我不知道怎么把自己的数据更新上去,就只能显示test的数据。
请帮帮我吧!

  • 写回答

2条回答 默认 最新

  • 社区专家-Monster-XH 2023-04-16 16:07
    关注

    基于Monster 组和GPT的调写:
    在使用mayavi嵌入pyqt5之后,要更新数据,可以使用以下步骤:

    1.在Visualization类中添加一个公共方法,用于接收要更新的数据

    class Visualization(HasTraits):
        scene = Instance(MlabSceneModel, ())
    
        def update_data(self, data):
            # Do something with the new data, for example:
            self.scene.mlab.clf()
            self.scene.mlab.contour3d(data)
    
        # the layout of the dialog screated
        view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
                         height=250, width=300, show_label=False),
                    resizable=True # We need this to resize with the parent widget
                    )
    
    
    

    2.在MayaviQWidget类中添加一个方法,用于调用Visualization的update_data方法

    class MayaviQWidget(QtGui.QWidget):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            layout = QtGui.QVBoxLayout(self)
            layout.setContentsMargins(0,0,0,0)
            layout.setSpacing(0)
            self.visualization = Visualization()
    
            # The edit_traits call will generate the widget to embed.
            self.ui = self.visualization.edit_traits(parent=self,
                                                     kind='subpanel').control
            layout.addWidget(self.ui)
            self.ui.setParent(self)
    
        def update_data(self, data):
            self.visualization.update_data(data)
    
    
    

    3.在主窗口中创建MayaviQWidget对象,并调用其update_data方法

    mayavi_widget = MayaviQWidget(container)
    container.show()
    window = QtGui.QMainWindow()
    window.setCentralWidget(container)
    window.show()
    
    # Update the data
    new_data = some_function_to_get_new_data()
    mayavi_widget.update_data(new_data)
    
    # Start the main event loop.
    app.exec_()
    
    
    

    这样就可以更新mayavi绘制的图形了。注意,在update_data方法中,要根据具体的需求对数据进行处理,并在Mayavi的场景(scene)中重新绘制图形。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月2日
  • 已采纳回答 4月24日
  • 创建了问题 4月16日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。