| 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 |
Button editor |
| 13 |
|
| 14 |
A Button trait is displayed as a button in a Traits UI view. When the button is |
| 15 |
clicked, Traits UI will execute a method of your choice (a 'listener'). |
| 16 |
|
| 17 |
In this example, the listener just increments a click counter. |
| 18 |
|
| 19 |
Please refer to the `ButtonEditor API docs`_ for further information. |
| 20 |
|
| 21 |
.. _ButtonEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.button_editor.html#traitsui.editors.button_editor.ButtonEditor |
| 22 |
""" |
| 23 |
|
| 24 |
from traits.api import HasTraits, Button, Int, observe |
| 25 |
|
| 26 |
from traitsui.api import Item, View |
| 27 |
|
| 28 |
|
| 29 |
class ButtonEditorDemo(HasTraits): |
| 30 |
"""Defines the main ButtonEditor demo class.""" |
| 31 |
|
| 32 |
# Define a Button trait: |
| 33 |
my_button_trait = Button('Click Me') |
| 34 |
click_counter = Int(0) |
| 35 |
|
| 36 |
# When the button is clicked, do something. |
| 37 |
@observe("my_button_trait") |
| 38 |
def _increment_counter(self, event): |
| 39 |
self.click_counter += 1 |
| 40 |
|
| 41 |
# Demo view: |
| 42 |
traits_view = View( |
| 43 |
'my_button_trait', |
| 44 |
Item('click_counter', style='readonly'), |
| 45 |
title='ButtonEditor', |
| 46 |
buttons=['OK'], |
| 47 |
resizable=True, |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
# Create the demo: |
| 52 |
demo = ButtonEditorDemo() |
| 53 |
|
| 54 |
# Run the demo (if invoked from the command line): |
| 55 |
if __name__ == '__main__': |
| 56 |
demo.configure_traits() |