Changing trait views on the fly

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Changing trait views on the fly

Brad Buran
Based on prior discussions on this mailing list (http://thread.gmane.org/gmane.comp.python.enthought.devel/17748 and http://thread.gmane.org/gmane.comp.python.enthought.devel/12391), I'm trying to create code that adds/removes editors from a view based on user interaction.  This is a simple example that I cannot get to work (the actual use case is detailed below).  Could anyone help point out what I'm doing wrong?

from enthought.traits.api import *
from enthought.traits.ui.api import *

class CustomController(Handler):

    def object_a_changed(self, info):
        if info.initialized:
            info.object.trait_view('view').updated = True
            info.ui.view.updated = True
            info.ui.updated = True

class CustomSettings(HasTraits):

    a = Int
    b = Int
    c = Int
    count = 0

    def view(self, parent=None):
        self.count += 1
        print 'Updating with count =', self.count
        if self.count == 1:
            return View('a')
        elif self.count == 2:
            return View('a', 'b')
        elif self.count == 3:
            return View('a', 'b', 'c')

if __name__ == '__main__':
    #ShockSettings().configure_traits(handler=ShockController())
    CustomSettings().configure_traits(handler=CustomController(), view='view')

The reason I'm trying to get this to work is because I need an arbitrary number of range editors to be displayed at run-time.  Each range editor corresponds to a variable.  As the user adds/removes variables during their experiment, I need to add/remove the corresponding range slider.  Note that the number of possible variables is not known before hand (otherwise I would use visible_when).

Thanks,
Brad

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Changing trait views on the fly

bryce hendrix-2
Brad, from what you are trying to do, it sounds like a combination of a ListEditor and InstanceEditors is what you want. Something like this:

class A(HasTraits):
    min = Int
    max = Int
    value = Int

    traits_view = View(Item('value', editor=RangeEditor(low_name='min', high_name='max'))

class B(HasTraits):
    ranges = List(A)

    traits_view = View(Item('ranges', editor=ListEditor(editor=InstanceEditor(), style='custom')))


On 05/03/2010 08:10 PM, Brad Buran wrote:
Based on prior discussions on this mailing list (http://thread.gmane.org/gmane.comp.python.enthought.devel/17748 and http://thread.gmane.org/gmane.comp.python.enthought.devel/12391), I'm trying to create code that adds/removes editors from a view based on user interaction.  This is a simple example that I cannot get to work (the actual use case is detailed below).  Could anyone help point out what I'm doing wrong?

from enthought.traits.api import *
from enthought.traits.ui.api import *

class CustomController(Handler):

    def object_a_changed(self, info):
        if info.initialized:
            info.object.trait_view('view').updated = True
            info.ui.view.updated = True
            info.ui.updated = True

class CustomSettings(HasTraits):

    a = Int
    b = Int
    c = Int
    count = 0

    def view(self, parent=None):
        self.count += 1
        print 'Updating with count =', self.count
        if self.count == 1:
            return View('a')
        elif self.count == 2:
            return View('a', 'b')
        elif self.count == 3:
            return View('a', 'b', 'c')

if __name__ == '__main__':
    #ShockSettings().configure_traits(handler=ShockController())
    CustomSettings().configure_traits(handler=CustomController(), view='view')

The reason I'm trying to get this to work is because I need an arbitrary number of range editors to be displayed at run-time.  Each range editor corresponds to a variable.  As the user adds/removes variables during their experiment, I need to add/remove the corresponding range slider.  Note that the number of possible variables is not known before hand (otherwise I would use visible_when).

Thanks,
Brad
_______________________________________________ Enthought-Dev mailing list [hidden email] https://mail.enthought.com/mailman/listinfo/enthought-dev


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev