4

I would like to set up an interface such that, upon listening to a trait, the interface will switch between different predefined View objects. Here is an example:

class App(Handler):
    info=Instance(UIInfo)
    view_type=Enum('slider_view','spinner_view')
    number=Range(1,10)
    message=Str('sawmill')

    spinner_view=View(Item('number',editor=RangeEditor(mode='spinner',high=10,  low=1)),Item('message'),Item('view_type'))
    slider_view=View(Item('number',editor=RangeEditor(mode='slider',high=10,    low=1)),Item('message'),Item('view_type'))

    @on_trait_change('view_type')
    def chg_view_type(self):
        self.reconstruct()

    def init_info(self,info):
        self.info=info
    def reconstruct(self):
        self.info.ui.dispose()
        self.info.object.edit_traits(view=self.view_type)

This code works and does everything that it needs to do for the toy example presented. However, my application has complex editors (including but not necessarily limited to a SceneEditor for a mayavi scene), that destroy their entire contents when disposed of. I am wondering if there is a simpler way of dynamically changing the view of a particular Item or subpanel (altering the view of an Instance that does not change would be just fine) while a window is active, whether by simplicity or trickery.

aestrivex
  • 4,640
  • 1
  • 23
  • 41

1 Answers1

0

If all you need is a dynamic visibility, this should do: https://github.com/enthought/traitsui/blob/master/examples/demo/Dynamic_Forms/visible_when.py

Jonathan March
  • 5,775
  • 2
  • 12
  • 16
  • I need the ability to switch dynamically from one `View` to another. It may be possible to concatenate all of the views into one big view and control them using `visible_when.` But I have had issues getting things to layout nicely when doing this -- sometimes Items do not show up until the window is rebuilt or show up in the wrong part of the window. This is part of what I was hoping to avoid if there were a way to dynamically switch between views. – aestrivex Nov 25 '13 at 15:41
  • I was playing around with this suggestion trying to see how much mileage I could get out of it. The result is super-ugly code, but I think that in principle this solution might work if not for the idiosyncracies of `SceneEditor`. `SceneEditor`s don't allow for other SceneEditors to display their same scenes, whether or not those scenes are currently enabled. I think this directly clashes with `visible_when` in my use case, but not in the desired solution of being able to define the editor in one view and then switching between views as needed. – aestrivex Dec 03 '13 at 21:14