Mayavi colorbar in TraitsUI creating blank window

PythonPython 3.xEnthoughtMayaviTraitsui

Python Problem Overview


I'm trying to create a GUI in TraitsUI that includes two Mayavi figures. I have implemented these figures as per the multiple engines example in the Mayavi documentation.

However, when I add a colorbar to one of the figures and run the GUI script it sometimes opens a blank Mayavi Scene Editor window in addition to the desired TraitsUI window. This blank window doesn't always appear, never on the first run after restarting the python kernel, and sometimes only after running the script a few times in succession and closing the windows that appear each time.

Running the much-reduced code below produces the same behaviour, and removing the mlab.colorbar(s) line stops the problem. How can I get a colorbar without opening blank windows? There doesn't seem to be an obvious way to assign a colorbar to a specific figure as for the surface plot. I am running Python 3.5 on Windows 7 (but get the same issues on 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()

Python Solutions


Solution 1 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSiyhView Question on Stackoverflow
Solution 1 - PythonAlexander BrockmeierView Answer on Stackoverflow