简体   繁体   中英

How to change traitsui view dynamically in active window

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.

如果您只需要动态可见性,那么应该这样做: https//github.com/enthought/traitsui/blob/master/examples/demo/Dynamic_Forms/visible_when.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM