Updating a trait view dynamically

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

Updating a trait view dynamically

Amit Aides
Hello,

I am trying to update a trait view dynamically.
Attached is a demo example. This example updates the range control when
some file is dropped into one of the drop controls.
I am using EPD 7.3.2 64 bit on a windows 7.

I notice two problems:
1) If I drop a file into the first drop control and then to the second,
I get the following error:

Traceback (most recent call last):
   File "C:\Python27\lib\site-packages\pyface\wx\drag_and_drop.py", line
198, in OnData
     x, y, clipboard.data, default_drag_result
   File "C:\Python27\lib\site-packages\traitsui\wx\drop_editor.py", line
116, in wx_dropped_on
     self.control.SetValue( self.str_value )
AttributeError: 'NoneType' object has no attribute 'SetValue'

2) Once this error occurs, closing the gui doesn't quit the program, and
I need to use Ctrl-C.

Also, is this the correct way to update a view control dynamically?

Thanks in advance,
Amit

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

drop.py (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Updating a trait view dynamically

Pietro Berkes
Hi Amit,

to update the view dynamically there is no need to mess with the traits internals. You can define the limits of 'tools' as traits themselves:

    lower_limit = Int(0)
    upper_limit = Int(1)
    tool = Range('lower_limit', 'upper_limit', 0)

and then update those after a drop.

The final code looks like this:

from traits.api import HasTraits, Int, Range, List, Instance, on_trait_change
from traitsui.api import View, Item, DropEditor
from enthought.io.api import File


class Demo(HasTraits):

    lower_limit = Int(0)
    upper_limit = Int(1)
    tool = Range('lower_limit', 'upper_limit', 0)
    drop1 = List(Instance(File))
    drop2 = List(Instance(File))
    
    view = View(
        Item('tool'),
        Item('drop1', editor=DropEditor()),
        Item('drop2', editor=DropEditor())
        )
    
    @on_trait_change('drop1')
    def update_view_1(self):
        self.upper_limit = 8

    @on_trait_change('drop2')
    def update_view_2(self):
        self.upper_limit = 16
    
if __name__ == '__main__':
    Demo().configure_traits()

Best,
Pietro


On Fri, Jan 18, 2013 at 8:17 AM, Amit Aides <[hidden email]> wrote:
Hello,

I am trying to update a trait view dynamically.
Attached is a demo example. This example updates the range control when some file is dropped into one of the drop controls.
I am using EPD 7.3.2 64 bit on a windows 7.

I notice two problems:
1) If I drop a file into the first drop control and then to the second, I get the following error:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\pyface\wx\drag_and_drop.py", line 198, in OnData
    x, y, clipboard.data, default_drag_result
  File "C:\Python27\lib\site-packages\traitsui\wx\drop_editor.py", line 116, in wx_dropped_on
    self.control.SetValue( self.str_value )
AttributeError: 'NoneType' object has no attribute 'SetValue'

2) Once this error occurs, closing the gui doesn't quit the program, and I need to use Ctrl-C.

Also, is this the correct way to update a view control dynamically?

Thanks in advance,
Amit

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




--
Pietro Berkes
Scientific software developer
Enthought UK


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

Re: Updating a trait view dynamically

Amit Aides
Thanks Pitero,

Works like a charm. There seems to be a small issue when the upper limit changes to a 3 digits number, part if it is left covered by the textual field (see attached example).

Thanks a lot,
Amit

On 18/01/2013 14:47, Pietro Berkes wrote:
from traits.api import HasTraits, Int, Range, List, Instance, on_trait_change
from traitsui.api import View, Item, DropEditor
from enthought.io.api import File


class Demo(HasTraits):

    lower_limit = Int(0)
    upper_limit = Int(1)
    tool = Range('lower_limit', 'upper_limit', 0)
    drop1 = List(Instance(File))
    drop2 = List(Instance(File))
    
    view = View(
        Item('tool'),
        Item('drop1', editor=DropEditor()),
        Item('drop2', editor=DropEditor())
        )
    
    @on_trait_change('drop1')
    def update_view_1(self):
        self.upper_limit = 8

    @on_trait_change('drop2')
    def update_view_2(self):
        self.upper_limit = 16
    
if __name__ == '__main__':
    Demo().configure_traits()



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

drop.py (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Updating a trait view dynamically

Pietro Berkes
Since you're using wx, you can set the 'label_width' attribute of the range editor. The with is in pixels.


    lower_limit = Int(0)
    upper_limit = Int(1)
    tool = Range(0)
    drop1 = List(Instance(File))
    drop2 = List(Instance(File))
    
    view = View(
        Item('tool', editor=RangeEditor(low_name='lower_limit',
                                        high_name='upper_limit',
                                        mode='slider',
                                        label_width=40)),
        Item('drop1', editor=DropEditor()),
        Item('drop2', editor=DropEditor()),
        resizable=True,
       )



On Fri, Jan 18, 2013 at 3:52 PM, Amit Aides <[hidden email]> wrote:
Thanks Pitero,

Works like a charm. There seems to be a small issue when the upper limit changes to a 3 digits number, part if it is left covered by the textual field (see attached example).

Thanks a lot,
Amit


On 18/01/2013 14:47, Pietro Berkes wrote:
from traits.api import HasTraits, Int, Range, List, Instance, on_trait_change
from traitsui.api import View, Item, DropEditor
from enthought.io.api import File


class Demo(HasTraits):

    lower_limit = Int(0)
    upper_limit = Int(1)
    tool = Range('lower_limit', 'upper_limit', 0)
    drop1 = List(Instance(File))
    drop2 = List(Instance(File))
    
    view = View(
        Item('tool'),
        Item('drop1', editor=DropEditor()),
        Item('drop2', editor=DropEditor())
        )
    
    @on_trait_change('drop1')
    def update_view_1(self):
        self.upper_limit = 8

    @on_trait_change('drop2')
    def update_view_2(self):
        self.upper_limit = 16
    
if __name__ == '__main__':
    Demo().configure_traits()



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




--
Pietro Berkes
Scientific software developer
Enthought UK


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