PyQt5 中的 Mayavi 集成:类的含义



我找到了一种方法将mayavi图集成到pyqt5 gui中。

import os
os.environ['ETS_TOOLKIT'] = 'qt5'
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
)

QWidget包含可视化,这是纯PyQt4代码。

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_()

我的问题:因为我是 python 的初学者,所以我真的不明白类可视化(( 是做什么的?有没有办法在不创建此类的情况下运行代码。 我问是因为我有一个现有的 GUI,我想在其中整合一个 mayavi 情节。在用 matplotlib 和画布制作的情节之前......所以我使用这段代码作为将情节嵌入到我的 gui 的基础。我删除了可视化类,但它得到错误:AttributeError: 'MainWindow' object has no attribute 'trait_view'。我认为它正在我的班级"主窗口"中搜索trait_view。我不明白为什么,因为我没有使用属性"trait_view"......

它实际上通过trait_view寻找view

traits是一个库,通过在访问成员时调用 pre 和 post 函数来提供类型化成员。不仅如此,还有一种通过python代码甚至GUI小部件与这些特征进行交互的可扩展方式(更多信息在这里(

您需要可视化通过这些特征提供用户与 GUI 小部件的交互性。

相关内容

  • 没有找到相关文章

最新更新