在 TraitsUI 中创建空白窗口

我试图在 TraitsUI 中创建一个包含两个 Mayavi 图形的 GUI。我已经按照 Mayavi 文档中的 多引擎例子实现了这些数字。

但是,当我添加一个颜色条到其中一个图形并运行 GUI 脚本时,除了所需的 TraitsUI 窗口之外,有时还会打开一个空白的 Mayavi Scene Editor 窗口。这个空白窗口并不总是出现,从来不会在重新启动 Python 内核后的第一次运行中出现,有时只是在连续运行脚本几次并关闭每次出现的窗口之后出现。

运行下面大大简化的代码会产生相同的行为,删除 mlab.colorbar(s)行就可以停止问题。我怎样才能得到一个不打开空白窗口的颜色栏?似乎没有一个明显的方法来指定一个特定的图形的颜色条作为表面的情节。我在 Windows 7上运行 Python 3.5(但在 Ubuntu 上也会遇到同样的问题)。

from traits.api import HasTraits, Instance, on_trait_change
from traitsui.api import View, Item
import numpy as np


from mayavi.core.api import Engine
from mayavi.core.ui.api import SceneEditor, MlabSceneModel
from mayavi import mlab


#Generate a test surface to display
def test_surf():
x, y = np.mgrid[-7.:7.05:0.1, -5.:5.05:0.05]
z = np.sin(x + y) + np.sin(2 * x - y) + np.cos(3 * x + 4 * y)
return x, y, z


class MyApp(HasTraits):


#Create a mayavi scene with a specified engine
engine = Instance(Engine, ())
scene = Instance(MlabSceneModel)
def _scene_default(self):
self.engine.start()
return MlabSceneModel(engine=self.engine)


#Plot the surface when the scene is activated
@on_trait_change('scene.activated')
def populate_scene(self):
s = mlab.surf(*test_surf(), figure=self.scene.mayavi_scene)
mlab.colorbar(s)


view = View(Item('scene', editor=SceneEditor()))


if __name__ == '__main__':
MyApp().configure_traits()
911 次浏览

You may add something that closes/quits the windows you invoke.

For example you could close the figure self.scene.mayavi_scene using function mayavi.mlab.close.