Enaml: dynamically generated check boxes??

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

Enaml: dynamically generated check boxes??

Neil Hodgson
Hi,

I am trying to dynamically generate a group of check boxes using Enaml.  The number of check boxes can be different each time the main window is created.  I tried to dig down and do something similar to a RadioGroup after looking at the code, but something is going wrong.

The check boxes don't always respond to the toggled attribute like I would expect.  Is it something I am doing wrong (likely) or a bug (less likely)?  Or is there a better way to do it?

Thanks,
Neil

enamldef _CheckBox(CheckBox):
    """ when toggled it should send it's idx attribute to the RangeGroup idx
    """
    attr idx: int = -1
    toggled ::
        parent.parent.idx = idx

enamldef RangeGroup(Container):
    attr idx: int = -1
    attr nchecks: int = -1
    idx ::
        if int == -1:
            pass
        else:
            print 'index {} selected'.format(idx)
    Include:
        components << get_check_boxes(range(nchecks))   

def get_check_boxes(labels):
    """ A helper function to dynamically generate check boxes.
        labels: List(Int)
    """
    res = []
    for label in labels:
        res.append(
            _CheckBox(idx=label, text=str(label), checked=False)
        )
    return res

enamldef Main(MainWindow):
    title = "Checkbox Test"
    attr nranges: int = 4
    Container:
        constraints = [width >= 200, height >=100]
        RangeGroup:
            id: test
            nchecks = nranges
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Enaml: dynamically generated check boxes??

Corran Webster
Hi Neil,

you probably want to use events in this sort of situation:


enamldef GroupCheckBox(CheckBox):

    attr group

    attr idx

    

    text << group.labels[idx]

    toggled ::

        group.toggled(idx)



enamldef CheckGroup(Container):

    attr values

    attr labels = [str(value) for value in values]

    attr checked = [False]*len(labels)

    attr selection = [value for value, checked in zip(values, checked) if checked]

    event selection_changed

    

    event toggle

    

    toggle ::

        # call checkgroup.toggle(idx) to toggle a checkbox programatically

        idx = event.new

        checkboxes.components[idx].toggle()

    

    event toggled

    

    toggled ::

        # handle a checkbox being toggled

        idx = event.new

        checked[idx] = not checked[idx]

        self.selection = [value for value, checked in zip(values, checked) if checked]

        selection_changed(self.selection)

    

    Include:

        id: checkboxes

        components = [GroupCheckBox(group=parent, idx=i)

            for i in range(len(labels))]



enamldef Main(MainWindow):

    CheckGroup:

        values = range(10)

        selection_changed ::

            print selection


This could be improved (it doesn't handle changing the list of values, for example), but hopefully this gives you a starting point.

-- Corran


On Mon, Aug 27, 2012 at 4:08 PM, Neil Hodgson <[hidden email]> wrote:
Hi,

I am trying to dynamically generate a group of check boxes using Enaml.  The number of check boxes can be different each time the main window is created.  I tried to dig down and do something similar to a RadioGroup after looking at the code, but something is going wrong.

The check boxes don't always respond to the toggled attribute like I would expect.  Is it something I am doing wrong (likely) or a bug (less likely)?  Or is there a better way to do it?

Thanks,
Neil

enamldef _CheckBox(CheckBox):
    """ when toggled it should send it's idx attribute to the RangeGroup idx
    """
    attr idx: int = -1
    toggled ::
        parent.parent.idx = idx

enamldef RangeGroup(Container):
    attr idx: int = -1
    attr nchecks: int = -1
    idx ::
        if int == -1:
            pass
        else:
            print 'index {} selected'.format(idx)
    Include:
        components << get_check_boxes(range(nchecks))   

def get_check_boxes(labels):
    """ A helper function to dynamically generate check boxes.
        labels: List(Int)
    """
    res = []
    for label in labels:
        res.append(
            _CheckBox(idx=label, text=str(label), checked=False)
        )
    return res

enamldef Main(MainWindow):
    title = "Checkbox Test"
    attr nranges: int = 4
    Container:
        constraints = [width >= 200, height >=100]
        RangeGroup:
            id: test
            nchecks = nranges
_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Enaml: dynamically generated check boxes??

Neil Hodgson

Hi Corran, 

That's more than enough to go on.  

Many thanks,
Neil


From: Corran Webster <[hidden email]>
To: Neil Hodgson <[hidden email]>; [hidden email]
Sent: Tuesday, 28 August 2012, 0:00
Subject: Re: [Enthought-Dev] Enaml: dynamically generated check boxes??

Hi Neil,

you probably want to use events in this sort of situation:

enamldef GroupCheckBox(CheckBox):
    attr group
    attr idx
    
    text << group.labels[idx]
    toggled ::
        group.toggled(idx)


enamldef CheckGroup(Container):
    attr values
    attr labels = [str(value) for value in values]
    attr checked = [False]*len(labels)
    attr selection = [value for value, checked in zip(values, checked) if checked]
    event selection_changed
    
    event toggle
    
    toggle ::
        # call checkgroup.toggle(idx) to toggle a checkbox programatically
        idx = event.new
        checkboxes.components[idx].toggle()
    
    event toggled
    
    toggled ::
        # handle a checkbox being toggled
        idx = event.new
        checked[idx] = not checked[idx]
        self.selection = [value for value, checked in zip(values, checked) if checked]
        selection_changed(self.selection)
    
    Include:
        id: checkboxes
        components = [GroupCheckBox(group=parent, idx=i)
            for i in range(len(labels))]


enamldef Main(MainWindow):
    CheckGroup:
        values = range(10)
        selection_changed ::
            print selection

This could be improved (it doesn't handle changing the list of values, for example), but hopefully this gives you a starting point.

-- Corran


On Mon, Aug 27, 2012 at 4:08 PM, Neil Hodgson <[hidden email]> wrote:
Hi,

I am trying to dynamically generate a group of check boxes using Enaml.  The number of check boxes can be different each time the main window is created.  I tried to dig down and do something similar to a RadioGroup after looking at the code, but something is going wrong.

The check boxes don't always respond to the toggled attribute like I would expect.  Is it something I am doing wrong (likely) or a bug (less likely)?  Or is there a better way to do it?

Thanks,
Neil

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

Re: Enaml: dynamically generated check boxes??

Jaidev Deshpande
In reply to this post by Corran Webster
On Tue, Aug 28, 2012 at 3:30 AM, Corran Webster <[hidden email]> wrote:

> Hi Neil,
>
> you probably want to use events in this sort of situation:
>
>
> enamldef GroupCheckBox(CheckBox):
>
>     attr group
>
>     attr idx
>
>
>
>     text << group.labels[idx]
>
>     toggled ::
>
>         group.toggled(idx)
>
>
>
> enamldef CheckGroup(Container):
>
>     attr values
>
>     attr labels = [str(value) for value in values]
>
>     attr checked = [False]*len(labels)
>
>     attr selection = [value for value, checked in zip(values, checked) if
> checked]
>
>     event selection_changed
>
>
>
>     event toggle
>
>
>
>     toggle ::
>
>         # call checkgroup.toggle(idx) to toggle a checkbox programatically
>
>         idx = event.new
>
>         checkboxes.components[idx].toggle()
>
>
>
>     event toggled
>
>
>
>     toggled ::
>
>         # handle a checkbox being toggled
>
>         idx = event.new
>
>         checked[idx] = not checked[idx]
>
>         self.selection = [value for value, checked in zip(values, checked)
> if checked]
>
>         selection_changed(self.selection)
>
>
>
>     Include:
>
>         id: checkboxes
>
>         components = [GroupCheckBox(group=parent, idx=i)
>
>             for i in range(len(labels))]
>
>
>
> enamldef Main(MainWindow):
>
>     CheckGroup:
>
>         values = range(10)
>
>         selection_changed ::
>
>             print selection
>
>
> This could be improved (it doesn't handle changing the list of values, for
> example), but hopefully this gives you a starting point.
>
> -- Corran
>
>
> On Mon, Aug 27, 2012 at 4:08 PM, Neil Hodgson <[hidden email]>
> wrote:
>>
>> Hi,
>>
>> I am trying to dynamically generate a group of check boxes using Enaml.
>> The number of check boxes can be different each time the main window is
>> created.  I tried to dig down and do something similar to a RadioGroup after
>> looking at the code, but something is going wrong.
>>
>> The check boxes don't always respond to the toggled attribute like I would
>> expect.  Is it something I am doing wrong (likely) or a bug (less likely)?
>> Or is there a better way to do it?
>>
>> Thanks,
>> Neil
>>
>> enamldef _CheckBox(CheckBox):
>>     """ when toggled it should send it's idx attribute to the RangeGroup
>> idx
>>     """
>>     attr idx: int = -1
>>     toggled ::
>>         parent.parent.idx = idx
>>
>> enamldef RangeGroup(Container):
>>     attr idx: int = -1
>>     attr nchecks: int = -1
>>     idx ::
>>         if int == -1:
>>             pass
>>         else:
>>             print 'index {} selected'.format(idx)
>>     Include:
>>         components << get_check_boxes(range(nchecks))
>>
>> def get_check_boxes(labels):
>>     """ A helper function to dynamically generate check boxes.
>>         labels: List(Int)
>>     """
>>     res = []
>>     for label in labels:
>>         res.append(
>>             _CheckBox(idx=label, text=str(label), checked=False)
>>         )
>>     return res
>>
>> enamldef Main(MainWindow):
>>     title = "Checkbox Test"
>>     attr nranges: int = 4
>>     Container:
>>         constraints = [width >= 200, height >=100]
>>         RangeGroup:
>>             id: test
>>             nchecks = nranges
>> _______________________________________________
>> 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
>

Hi,

I have a similar question about Form widgets. I want the code to deal
with a variable number of child pairs in a Form widget.

Consider this:

enamldef Main(MainWindow):
    attr model
    Form:
        ....
        ....
        ....
        # Pairs of widgets, mostly label - field pairs
        ....
        ....
        ....

Suppose the model attribute is a 2-D array like structure and the
number of child pairs I want in the form is equal to the number of
columns in the array. How should I go about it?

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

Re: Enaml: dynamically generated check boxes??

Jaidev Deshpande
In reply to this post by Neil Hodgson
On Tue, Aug 28, 2012 at 2:38 AM, Neil Hodgson <[hidden email]> wrote:

> Hi,
>
> I am trying to dynamically generate a group of check boxes using Enaml.  The number of check boxes can be different each time the main window is created.  I tried to dig down and do something similar to a RadioGroup after looking at the code, but something is going wrong.
>
> The check boxes don't always respond to the toggled attribute like I would expect.  Is it something I am doing wrong (likely) or a bug (less likely)?  Or is there a better way to do it?
>
> Thanks,
> Neil
>
> enamldef _CheckBox(CheckBox):
>     """ when toggled it should send it's idx attribute to the RangeGroup idx
>     """
>     attr idx: int = -1
>     toggled ::
>         parent.parent.idx = idx
>
> enamldef RangeGroup(Container):
>     attr idx: int = -1
>     attr nchecks: int = -1
>     idx ::
>         if int == -1:
>             pass
>         else:
>             print 'index {} selected'.format(idx)
>     Include:
>         components << get_check_boxes(range(nchecks))

Hi Neil,

in the above definition, what is the Include attribute?

Thanks

>
> def get_check_boxes(labels):
>     """ A helper function to dynamically generate check boxes.
>         labels: List(Int)
>     """
>     res = []
>     for label in labels:
>         res.append(
>             _CheckBox(idx=label, text=str(label), checked=False)
>         )
>     return res
>
> enamldef Main(MainWindow):
>     title = "Checkbox Test"
>     attr nranges: int = 4
>     Container:
>         constraints = [width >= 200, height >=100]
>         RangeGroup:
>             id: test
>             nchecks = nranges
> _______________________________________________
> 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
Reply | Threaded
Open this post in threaded view
|

Re: Enaml: dynamically generated check boxes??

Puneeth Chaganti
On Tue, Aug 28, 2012 at 9:23 AM, Jaidev Deshpande
<[hidden email]> wrote:
>
> in the above definition, what is the Include attribute?

It's a component in Enaml that lets you dynamically add children under
a parent.  For more, look at enaml/components/include.py and
examples/components/{inline,include}.enaml.

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

Re: Enaml: dynamically generated check boxes??

Neil Hodgson
In reply to this post by Jaidev Deshpande


Jaidev,

Yes, the docs are not very comprehensive yet.  Still, I am impressed by how far you can get with the stuff that is there.  It says something about how intuitive the library is. 

I've been looking at the examples.  If there's something in there I can't understand and there aren't docs for it I look in the code (which is very well documented).

I found Include in the Radio Group example - the docstring reveals:

class Include(BaseComponent):
    """ A BaseComponent subclass which allows children to be dynamically
included into a parent.

"""

You can dynamically generate components and add them to the components attribute of the Include.

Best wishes

Neil

________________________________
From: Jaidev Deshpande <[hidden email]>
To: Neil Hodgson <[hidden email]>; [hidden email]
Sent: Tuesday, 28 August 2012, 5:53
Subject: Re: [Enthought-Dev] Enaml: dynamically generated check boxes??

On Tue, Aug 28, 2012 at 2:38 AM, Neil Hodgson <[hidden email]> wrote:

> Hi,
>
> I am trying to dynamically generate a group of check boxes using Enaml.  The number of check boxes can be different each time the main window is created.  I tried to dig down and do something similar to a RadioGroup after looking at the code, but something is going wrong.
>
> The check boxes don't always respond to the toggled attribute like I would expect.  Is it something I am doing wrong (likely) or a bug (less likely)?  Or is there a better way to do it?
>
> Thanks,
> Neil
>
> enamldef _CheckBox(CheckBox):
>     """ when toggled it should send it's idx attribute to the RangeGroup idx
>     """
>     attr idx: int = -1
>     toggled ::
>         parent.parent.idx = idx
>
> enamldef RangeGroup(Container):
>     attr idx: int = -1
>     attr nchecks: int = -1
>     idx ::
>         if int == -1:
>             pass
>         else:
>             print 'index {} selected'.format(idx)
>     Include:
>         components << get_check_boxes(range(nchecks))

Hi Neil,

in the above definition, what is the Include attribute?

Thanks

>
> def get_check_boxes(labels):
>     """ A helper function to dynamically generate check boxes.
>         labels: List(Int)
>     """
>     res = []
>     for label in labels:
>         res.append(
>             _CheckBox(idx=label, text=str(label), checked=False)
>         )
>     return res
>
> enamldef Main(MainWindow):
>     title = "Checkbox Test"
>     attr nranges: int = 4
>     Container:
>         constraints = [width >= 200, height >=100]
>         RangeGroup:
>             id: test
>             nchecks = nranges
> _______________________________________________
> 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