| 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 |
This demo shows how to dynamically change the label or image of a button. Note |
| 13 |
in this demo clicking the button itself does nothing. |
| 14 |
|
| 15 |
Please refer to the `ButtonEditor API docs`_ for further information. |
| 16 |
|
| 17 |
.. _ButtonEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.button_editor.html#traitsui.editors.button_editor.ButtonEditor |
| 18 |
""" |
| 19 |
from pyface.api import Image, ImageResource |
| 20 |
from traits.api import Button, Enum, HasTraits, Instance, List, Str |
| 21 |
|
| 22 |
from traitsui.api import ( |
| 23 |
ButtonEditor, |
| 24 |
Group, |
| 25 |
ImageEditor, |
| 26 |
InstanceChoice, |
| 27 |
InstanceEditor, |
| 28 |
Item, |
| 29 |
UItem, |
| 30 |
View, |
| 31 |
) |
| 32 |
import traitsui.extras |
| 33 |
|
| 34 |
|
| 35 |
class ImageChoice(InstanceChoice): |
| 36 |
def get_view(self): |
| 37 |
return View(UItem('name', editor=ImageEditor(image=self.object))) |
| 38 |
|
| 39 |
|
| 40 |
class ButtonEditorDemo(HasTraits): |
| 41 |
my_button = Button() |
| 42 |
|
| 43 |
my_button_label = Str("Initial Label") |
| 44 |
|
| 45 |
my_button_image = Image() |
| 46 |
|
| 47 |
my_button_image_options = List( |
| 48 |
Image(), |
| 49 |
value=[ |
| 50 |
ImageResource("run", [traitsui.extras]), |
| 51 |
ImageResource("previous", [traitsui.extras]), |
| 52 |
ImageResource("next", [traitsui.extras]), |
| 53 |
ImageResource("parent", [traitsui.extras]), |
| 54 |
ImageResource("reload", [traitsui.extras]), |
| 55 |
], |
| 56 |
) |
| 57 |
|
| 58 |
def _my_button_image_default(self): |
| 59 |
return self.my_button_image_options[0] |
| 60 |
|
| 61 |
traits_view = View( |
| 62 |
Item( |
| 63 |
"my_button", |
| 64 |
style="custom", |
| 65 |
editor=ButtonEditor( |
| 66 |
label_value="my_button_label", |
| 67 |
image_value="my_button_image", |
| 68 |
orientation="horizontal", |
| 69 |
), |
| 70 |
), |
| 71 |
Item("my_button_label"), |
| 72 |
Item( |
| 73 |
"my_button_image", |
| 74 |
editor=InstanceEditor( |
| 75 |
name="my_button_image_options", adapter=ImageChoice |
| 76 |
), |
| 77 |
style="custom", |
| 78 |
), |
| 79 |
) |
| 80 |
|
| 81 |
|
| 82 |
# Create the demo: |
| 83 |
demo = ButtonEditorDemo() |
| 84 |
|
| 85 |
# Run the demo (if invoked from the command line): |
| 86 |
if __name__ == '__main__': |
| 87 |
demo.configure_traits() |