| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" |
| 12 |
Spacing widgets using springs |
| 13 |
|
| 14 |
*Springs* are a simple technique for adding space before, after, or between |
| 15 |
Traits UI editors (a.k.a. widgets). |
| 16 |
|
| 17 |
By default, Traits UI arranges widgets immediately adjacent to each other -- |
| 18 |
from left to right in a horizontal group, or from top to bottom in a vertical |
| 19 |
group. Sometimes this works well, but sometimes it results in widgets that are |
| 20 |
placed confusingly, or have been unattractively stretched to fill available |
| 21 |
space. |
| 22 |
|
| 23 |
When you place a *spring* in a horizontal group, Traits UI will not try to fill |
| 24 |
that space with an adjacent widget, instead allowing empty space which varies |
| 25 |
depending on the overall size of the containing group. |
| 26 |
""" |
| 27 |
|
| 28 |
from traits.api import HasTraits, Button |
| 29 |
from traitsui.api import View, VGroup, HGroup, Item, spring, Label |
| 30 |
|
| 31 |
# dummy button which will be used repeatedly to demonstrate widget spacing: |
| 32 |
button = Item('ignore', show_label=False) |
| 33 |
|
| 34 |
|
| 35 |
class SpringDemo(HasTraits): |
| 36 |
|
| 37 |
ignore = Button('Ignore') |
| 38 |
|
| 39 |
view = View( |
| 40 |
VGroup( |
| 41 |
'10', |
| 42 |
Label(label='Spring in a horizontal group moves widget right:'), |
| 43 |
'10', |
| 44 |
HGroup( |
| 45 |
button, |
| 46 |
button, |
| 47 |
show_border=True, |
| 48 |
label='Left justified (no springs)', |
| 49 |
), |
| 50 |
HGroup( |
| 51 |
spring, |
| 52 |
button, |
| 53 |
button, |
| 54 |
show_border=True, |
| 55 |
label='Right justified with a spring ' 'before any buttons', |
| 56 |
), |
| 57 |
HGroup( |
| 58 |
button, |
| 59 |
spring, |
| 60 |
button, |
| 61 |
show_border=True, |
| 62 |
label='Left and right justified with a ' |
| 63 |
'spring between buttons', |
| 64 |
), |
| 65 |
HGroup( |
| 66 |
button, |
| 67 |
button, |
| 68 |
spring, |
| 69 |
button, |
| 70 |
button, |
| 71 |
spring, |
| 72 |
button, |
| 73 |
button, |
| 74 |
show_border=True, |
| 75 |
label='Left, center and right justified ' |
| 76 |
'with springs after the 2nd and 4th ' |
| 77 |
'buttons', |
| 78 |
), |
| 79 |
spring, |
| 80 |
Label( |
| 81 |
'Spring in vertical group moves widget down ' |
| 82 |
'(does not work on Wx backend).' |
| 83 |
), |
| 84 |
button, |
| 85 |
), |
| 86 |
width=600, |
| 87 |
height=600, |
| 88 |
resizable=True, |
| 89 |
title='Spring Demo', |
| 90 |
buttons=['OK'], |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
demo = SpringDemo() |
| 95 |
|
| 96 |
if __name__ == '__main__': |
| 97 |
demo.configure_traits() |