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
|
"""
Boolean editor (checkbox or text)
A Boolean (True/False) trait is displayed and edited as a checkbox, by default.
It can also be displayed as text 'True' / 'False', either editable or read-only.
This example also shows how to listen for a change in a trait, and take
action when its value changes.
It also demonstrates how to add vertical space and a Label (plain text which is
not editable.)
"""
from traits.api import HasTraits, Bool, Int
from traitsui.api import Item, Label, Group, View
class BooleanEditorDemo (HasTraits):
""" Defines the main BooleanEditor demo class. """
# a boolean trait to view:
my_boolean_trait = Bool
count_changes = Int(0)
# When the trait's value changes, do something.
# The listener method is named '_TraitName_changed', where
# 'TraitName' is the name of the trait being monitored.
def _my_boolean_trait_changed(self):
self.count_changes += 1
# Demo view
traits_view = View(
'10', # vertical space
# The trait to be displayed / edited, in default format.
# To edit a simple trait, this is the only line needed inside the View.
# This is shorthand for Item('my_boolean_trait', style = 'simple')
'my_boolean_trait',
'10', # vertical space
# We put this label in its own group so that it will be left justified.
# Otherwise it will line up with other edit fields (indented):
Group(
Label('The same Boolean trait can also be displayed and edited as '
'text (True/False):')
),
'10', # vertical space
Item( 'my_boolean_trait', style = 'readonly', label = 'Read-only style' ),
Item( 'my_boolean_trait', style = 'text', label = 'Text style' ),
'10',
'count_changes',
title = 'Boolean trait',
buttons = ['OK'],
resizable = True
)
# Create the demo view (but do not yet display it):
demo = BooleanEditorDemo()
# Display and edit the demo (if invoked from the command line):
if __name__ == '__main__':
demo.configure_traits()
|