| 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 |
Dynamic Range editor |
| 13 |
|
| 14 |
Demonstrates how to dynamically modify the low and high limits of a Range Trait. |
| 15 |
|
| 16 |
In the simple *Range Editor* example, we saw how to define a Range Trait, whose |
| 17 |
values were restricted to a fixed range. Here, we show how the limits of the |
| 18 |
range can be changed dynamically using the editor's *low_name* and *high_name* |
| 19 |
attributes. |
| 20 |
|
| 21 |
In this example, these range limits are set with sliders. In practice, the |
| 22 |
limits would often be calculated from other user input or model data. |
| 23 |
|
| 24 |
The demo is divided into three tabs: |
| 25 |
|
| 26 |
* A dynamic range using a simple slider. |
| 27 |
* A dynamic range using a large-range slider. |
| 28 |
* A dynamic range using a spinner. |
| 29 |
|
| 30 |
In each section of the demo, the top-most 'value' trait can have its range |
| 31 |
end points changed dynamically by modifying the 'low' and 'high' sliders |
| 32 |
below it. |
| 33 |
|
| 34 |
The large-range slider includes low-end and high-end arrows, which are used |
| 35 |
to step the visible range through the full range, when the latter is too large |
| 36 |
to be entirely visible. |
| 37 |
|
| 38 |
This demo also illustrates how the value, label formatting and label |
| 39 |
widths can also be specified if desired. |
| 40 |
""" |
| 41 |
|
| 42 |
# Imports: |
| 43 |
from traits.api import HasPrivateTraits, Float, Range, Int |
| 44 |
|
| 45 |
from traitsui.api import View, Group, Item, Label, RangeEditor |
| 46 |
|
| 47 |
|
| 48 |
class DynamicRangeEditor(HasPrivateTraits): |
| 49 |
"""Defines an editor for dynamic ranges (i.e. ranges whose bounds can be |
| 50 |
changed at run time). |
| 51 |
""" |
| 52 |
|
| 53 |
# The value with the dynamic range: |
| 54 |
value = Float() |
| 55 |
|
| 56 |
# This determines the low end of the range: |
| 57 |
low = Range(0.0, 10.0, 0.0) |
| 58 |
|
| 59 |
# This determines the high end of the range: |
| 60 |
high = Range(20.0, 100.0, 20.0) |
| 61 |
|
| 62 |
# An integer value: |
| 63 |
int_value = Int() |
| 64 |
|
| 65 |
# This determines the low end of the integer range: |
| 66 |
int_low = Range(0, 10, 0) |
| 67 |
|
| 68 |
# This determines the high end of the range: |
| 69 |
int_high = Range(20, 100, 20) |
| 70 |
|
| 71 |
# Traits view definitions: |
| 72 |
traits_view = View( |
| 73 |
# Dynamic simple slider demo: |
| 74 |
Group( |
| 75 |
Item( |
| 76 |
'value', |
| 77 |
editor=RangeEditor( |
| 78 |
low_name='low', |
| 79 |
high_name='high', |
| 80 |
format_str='%.1f', |
| 81 |
label_width=28, |
| 82 |
mode='auto', |
| 83 |
), |
| 84 |
), |
| 85 |
'_', |
| 86 |
Item('low'), |
| 87 |
Item('high'), |
| 88 |
'_', |
| 89 |
Label( |
| 90 |
'Move the Low and High sliders to change the range of ' |
| 91 |
'Value.' |
| 92 |
), |
| 93 |
label='Simple Slider', |
| 94 |
), |
| 95 |
# Dynamic large range slider demo: |
| 96 |
Group( |
| 97 |
Item( |
| 98 |
'value', |
| 99 |
editor=RangeEditor( |
| 100 |
low_name='low', |
| 101 |
high_name='high', |
| 102 |
format_str='%.1f', |
| 103 |
label_width=28, |
| 104 |
mode='xslider', |
| 105 |
), |
| 106 |
), |
| 107 |
'_', |
| 108 |
Item('low'), |
| 109 |
Item('high'), |
| 110 |
'_', |
| 111 |
Label( |
| 112 |
'Move the Low and High sliders to change the range of ' |
| 113 |
'Value.' |
| 114 |
), |
| 115 |
label='Large Range Slider', |
| 116 |
), |
| 117 |
# Dynamic spinner demo: |
| 118 |
Group( |
| 119 |
Item( |
| 120 |
'int_value', |
| 121 |
editor=RangeEditor( |
| 122 |
low=0, |
| 123 |
high=20, |
| 124 |
low_name='int_low', |
| 125 |
high_name='int_high', |
| 126 |
format_str='%d', |
| 127 |
is_float=False, |
| 128 |
label_width=28, |
| 129 |
mode='spinner', |
| 130 |
), |
| 131 |
), |
| 132 |
'_', |
| 133 |
Item('int_low'), |
| 134 |
Item('int_high'), |
| 135 |
'_', |
| 136 |
Label( |
| 137 |
'Move the Low and High sliders to change the range of ' |
| 138 |
'Value.' |
| 139 |
), |
| 140 |
label='Spinner', |
| 141 |
), |
| 142 |
title='Dynamic Range Editor Demonstration', |
| 143 |
buttons=['OK'], |
| 144 |
resizable=True, |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
# Create the demo: |
| 149 |
demo = DynamicRangeEditor() |
| 150 |
|
| 151 |
# Run the demo (if invoked from the command line): |
| 152 |
if __name__ == '__main__': |
| 153 |
demo.configure_traits() |