When a Spring is not a spring

Mar 29 2010 Published by under Enthought Tool Suite

TraitsUI has a lot of layout magic, which is usually good enough, but sometimes you need to apply a little extra effort to get your GUI layout correct. One trick I often employ is to use springs between Items to align them. That works most of the time, but on occasion I need a fixed width spacer.

Lets look at the spring code:

class Spring ( Item ):
    # An item that is a layout "spring".

    # Name of the trait the item is editing
    name = 'spring'

    # Should a label be displayed?
    show_label = Bool( False )

    # Editor to use for the item
    editor = Instance( 'enthought.traits.ui.api.NullEditor', () )

    # Should the item use extra space along its Group's
    # layout orientation?
    springy = True

spring = Spring()

So what would happen if we tried to create a Spring with the width set and springy disabled? We get a fixed width spacer!

from enthought.traits.api import HasTraits, Int
from enthought.traits.ui.api import View, Item, VGroup, HGroup, \
     Spring, spring

class ExampleView(HasTraits):
    a = Int
    b = Int

    def trait_view(self, parent=None):
       regular_group = HGroup(
                         HGroup(Item('a'),
                                show_border=True),
                         HGroup(Item('b'),
                                show_border=True),
                         show_border=True,
                         label='regular')

       flex_group = HGroup(
                       HGroup(Item('a'),
                              show_border=True),
                       spring,
                       HGroup(Item('b'),
                              show_border=True),
                       show_border=True,
                       label='flexible')

       fixed_group = HGroup(
                       HGroup(Item('a'),
                              show_border=True),
                       Spring(width=200, springy=False),
                       HGroup(Item('b'),
                              show_border=True),
                       show_border=True,
                       label='fixed')

       return View(VGroup(regular_group,
                          flex_group,
                          fixed_group),
                   resizable=True)

ExampleView().configure_traits()

spring_example

3 responses so far

Leave a Reply

Featuring Advanced Search Functions plugin by YD