| 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 |
Boolean editor (checkbox or text) |
| 13 |
|
| 14 |
A Boolean (True/False) trait is displayed and edited as a checkbox, by default. |
| 15 |
|
| 16 |
It can also be displayed as text 'True'/'False', either editable or read-only. |
| 17 |
|
| 18 |
This example also shows how to listen for a change in a trait, and take |
| 19 |
action when its value changes. |
| 20 |
|
| 21 |
It also demonstrates how to add vertical space and a Label (plain text which is |
| 22 |
not editable.) |
| 23 |
|
| 24 |
Please refer to the `BooleanEditor API docs`_ for further information. |
| 25 |
|
| 26 |
.. _BooleanEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.boolean_editor.html#traitsui.editors.boolean_editor.BooleanEditor |
| 27 |
""" |
| 28 |
|
| 29 |
from traits.api import HasTraits, Bool, Int |
| 30 |
from traitsui.api import Item, Label, Group, View |
| 31 |
|
| 32 |
|
| 33 |
class BooleanEditorDemo(HasTraits): |
| 34 |
"""Defines the main BooleanEditor demo class.""" |
| 35 |
|
| 36 |
# a boolean trait to view: |
| 37 |
my_boolean_trait = Bool() |
| 38 |
count_changes = Int(0) |
| 39 |
|
| 40 |
# When the trait's value changes, do something. |
| 41 |
# The listener method is named '_TraitName_changed', where |
| 42 |
# 'TraitName' is the name of the trait being monitored. |
| 43 |
def _my_boolean_trait_changed(self): |
| 44 |
self.count_changes += 1 |
| 45 |
|
| 46 |
# Demo view |
| 47 |
traits_view = View( |
| 48 |
'10', # vertical space |
| 49 |
Item('my_boolean_trait', style='simple', id='simple'), |
| 50 |
'10', # vertical space |
| 51 |
# We put this label in its own group so that it will be left justified. |
| 52 |
# Otherwise it will line up with other edit fields (indented): |
| 53 |
Group( |
| 54 |
Label( |
| 55 |
'The same Boolean trait can also be displayed and edited as ' |
| 56 |
'text (True/False):' |
| 57 |
) |
| 58 |
), |
| 59 |
'10', # vertical space |
| 60 |
Item( |
| 61 |
'my_boolean_trait', |
| 62 |
style='readonly', |
| 63 |
label='Read-only style', |
| 64 |
id='readonly', |
| 65 |
), |
| 66 |
Item('my_boolean_trait', style='text', label='Text style', id='text'), |
| 67 |
'10', |
| 68 |
'count_changes', |
| 69 |
title='Boolean trait', |
| 70 |
buttons=['OK'], |
| 71 |
resizable=True, |
| 72 |
) |
| 73 |
|
| 74 |
|
| 75 |
# Create the demo view (but do not yet display it): |
| 76 |
demo = BooleanEditorDemo() |
| 77 |
|
| 78 |
# Display and edit the demo (if invoked from the command line): |
| 79 |
if __name__ == '__main__': |
| 80 |
demo.configure_traits() |