| 1 |
# (C) Copyright 2007-2025 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 |
""" A simple example of a GUIApplication which wraps a TraitsUI """ |
| 12 |
|
| 13 |
from traits.api import Enum, HasTraits, Instance, Int, on_trait_change, Str |
| 14 |
from traitsui.api import Item, OKCancelButtons, View |
| 15 |
|
| 16 |
from envisage.api import Plugin |
| 17 |
from envisage.ui.gui_application import GUIApplication |
| 18 |
|
| 19 |
|
| 20 |
class Person(HasTraits): |
| 21 |
"""A typical traits model object""" |
| 22 |
|
| 23 |
name = Str("John Doe") |
| 24 |
|
| 25 |
age = Int(21) |
| 26 |
|
| 27 |
gender = Enum("Male", "Female") |
| 28 |
|
| 29 |
view = View( |
| 30 |
Item("name"), |
| 31 |
Item("age"), |
| 32 |
Item("gender"), |
| 33 |
buttons=OKCancelButtons, |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
class PersonViewPlugin(Plugin): |
| 38 |
"""The 'Person View' plugin. |
| 39 |
|
| 40 |
This plugin waits for the application to start, and then creates a traits |
| 41 |
UI. |
| 42 |
|
| 43 |
""" |
| 44 |
|
| 45 |
ui = Instance("traitsui.ui.UI") |
| 46 |
|
| 47 |
@on_trait_change("application:application_initialized") |
| 48 |
def on_application_start(self): |
| 49 |
"""Start the UI.""" |
| 50 |
|
| 51 |
person = Person() |
| 52 |
|
| 53 |
# keep a reference to the ui object to avoid garbage collection |
| 54 |
self.ui = person.edit_traits() |
| 55 |
|
| 56 |
|
| 57 |
# Application entry point. |
| 58 |
if __name__ == "__main__": |
| 59 |
# Create the application. |
| 60 |
application = GUIApplication( |
| 61 |
id="person_view", plugins=[PersonViewPlugin()] |
| 62 |
) |
| 63 |
|
| 64 |
# Run it! |
| 65 |
application.run() |