Looper Example

An example of using Looper to generate widgets from an iterable.

Tip

To see this example in action, download it from looper and run:

$ enaml-run looper.enaml

Screenshot

../_images/ex_looper.png

Example Enaml Code

#------------------------------------------------------------------------------
# Copyright (c) 2013, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
""" An example of using Looper to generate widgets from an iterable.

<< autodoc-me >>
"""
from __future__ import print_function
from enaml.core.api import Looper
from enaml.layout.api import vbox, hbox, align
from enaml.widgets.api import (
    Window, Container, Label, Field, PushButton, ScrollArea, Slider, Html,
)


enamldef Main(Window):
    Container:
        constraints = [
            vbox(
                hbox(label, field),
                button,
                scroller,
            ),
            align('v_center', label, field),
        ]
        Label: label:
            text = 'Items'
        Field: field:
            text = 'foo bar baz spam ham'
        PushButton: button:
            text = 'Print Items'
            clicked ::
                for item in looper.items:
                    print(item)
        ScrollArea: scroller:
            Container:
                # Note that a Looper expects to iterate over unique values.
                # Passing duplicate values can lead to crashes.
                Looper: looper:
                    iterable << field.text.split()
                    Field:
                        placeholder = 'Field %d: %s' % (loop.index, loop.item)
                    PushButton:
                        text = 'pb %s' % loop.index
                    Slider:
                        value :: print('Slider %d changed' % loop.index)
                    Html:
                        source << '<h1><center>%s</center></h1>' % loop.item