从Mayavi场景中删除工具栏

时间:2018-08-22 20:34:32

标签: python toolbar mayavi

是否可以删除/隐藏使用mayavi绘制图形时默认创建的标准工具栏?我发现了这个:How to remove Mayavi toolbar in python using code

但是我不知道该在哪里插入这段代码。有像mlab.toolbar.setVisible(False)这样的东西更容易吗?

2 个答案:

答案 0 :(得分:0)

import os
os.environ['ETS_TOOLKIT'] = 'qt4'
os.environ['QT_API'] = 'pyqt5' 

from traits.api import HasTraits, Instance, on_trait_change
from traitsui.api import View, Item
from mayavi.core.ui.api import MayaviScene, MlabSceneModel, SceneEditor
from PyQt5.QtWidgets import *
from main import Ui_MainWindow
import sys
from traitsui.api import Handler


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

    @on_trait_change('scene.activated')
    def update_plot(self):
        self.scene.mlab.test_points3d()

    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
            )


class DisableToolbarHandler(Handler):
    def position(self, info):
        editor = info.ui.get_editors('scene')[0]
        editor._scene._tool_bar.setVisible(False)


class MayaviQWidget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        self.visualization = Visualization()
        self.ui = self.visualization.edit_traits(handler=DisableToolbarHandler(), kind='subpanel').control
        layout.addWidget(self.ui)
        self.ui.setParent(self)


class Action(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(Action, self).__init__(parent)
        self.setupUi(self)
        self.splitter.setSizes([100, 300])
        self.splitter_2.setSizes([400, 100])

        container = QWidget()
        mayavi_widget = MayaviQWidget(container)
        self.Layout.addWidget(mayavi_widget)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    action = Action()
    action.show()
    sys.exit(app.exec_())

答案 1 :(得分:0)

在图形上,可以很容易地像这样完成

fig = mlab.figure(1)
fig.scene.scene_editor._tool_bar.setVisible(False)