When a Spring is not a spring

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:
[sourcecode language=”python”]
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()
[/sourcecode]

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

[sourcecode language=”python”]
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()
[/sourcecode]

spring_example

3 thoughts on “When a Spring is not a spring

  1. Pingback: Tweets that mention When a Spring is not a spring -- Topsy.com

    1. avatarBryce Hendrix

      The screen shots are from Wx, though this should apply to Qt too. But, to be honest, I didn’t try the sample code I wrote in Qt.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 102,429 bad guys.