File: converter.py

package info (click to toggle)
python-traitsui 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 13,292 kB
  • sloc: python: 39,867; makefile: 120; sh: 5
file content (88 lines) | stat: -rw-r--r-- 3,180 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Tiny application: convert length measurements from one unit system to another

Select the input and output units using the drop down combo-boxes in the
*Input:* and *Output:* sections respectively. Type the input quantity
to convert into the left most text box. The output value corresponding to the
current input value will automatically be updated in the *Output:*
section.

Use the *Undo* and *ReDo* buttons to undo and redo changes you
have made to any of the input fields.

Note that other than the 'output_amount' property implementation, the rest
of the code is simply declarative.
"""

# FIXME: Help is broken in QT

from traits.api import HasStrictTraits, Trait, CFloat, Property
from traitsui.api import View, VGroup, HGroup, Item

# Help text:
ViewHelp = """
This program converts length measurements from one unit system to another.

Select the input and output units using the drop down combo-boxes in the
*Input:* and *Output:* sections respectively. Type the input quantity
to convert into the left most text box. The output value corresponding to the
current input value will automatically be updated in the *Output:*
section.

Use the *Undo* and *ReDo* buttons to undo and redo changes you
have made to any of the input fields.
"""

# Units trait maps all units to centimeters:
Units = Trait( 'inches', { 'inches':      2.54,
                           'feet':        (12 * 2.54),
                           'yards':       (36 * 2.54),
                           'miles':       (5280 * 12 * 2.54),
                           'millimeters': 0.1,
                           'centimeters': 1.0,
                           'meters':      100.0,
                           'kilometers':  100000.0 } )

# Converter Class:
class Converter ( HasStrictTraits ):

    # Trait definitions:
    input_amount  = CFloat( 12.0,    desc = "the input quantity" )
    input_units   = Units( 'inches', desc = "the input quantity's units" )
    output_amount = Property( depends_on = [ 'input_amount', 'input_units',
                                             'output_units' ],
                              desc = "the output quantity" )
    output_units  = Units( 'feet',   desc = "the output quantity's units" )

    # User interface views:
    traits_view = View(
        VGroup(
            HGroup(
                Item( 'input_amount', springy = True ),
                Item( 'input_units', show_label = False ),
                label       = 'Input',
                show_border = True
            ),
            HGroup(
                Item( 'output_amount', style = 'readonly', springy = True ),
                Item( 'output_units',  show_label = False ),
                label       = 'Output',
                show_border = True
            ),
            help = ViewHelp
        ),
        title   = 'Units Converter',
        buttons = [ 'Undo', 'OK', 'Help' ]
    )

    # Property implementations
    def _get_output_amount ( self ):
        return ((self.input_amount * self.input_units_) / self.output_units_)

# Create the demo:
popup = Converter()

# Run the demo (if invoked from the command line):
if __name__ == '__main__':
    popup.configure_traits()