| 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 |
Demonstrates using a popup view within another view. |
| 13 |
|
| 14 |
Try changing the gender of the person from 'Male' to 'Female' using the |
| 15 |
drop-down list. When the person's gender is changed, a pop-up dialog is |
| 16 |
displayed immediately below the gender field providing you with the |
| 17 |
opportunity to cancel the gender change. |
| 18 |
|
| 19 |
If you click the Cancel button, the person's gender will return to its previous |
| 20 |
value. If you click anywhere else outside of the pop-up dialog, the pop-up |
| 21 |
dialog will simply disappear, leaving the person's new gender value as is. |
| 22 |
|
| 23 |
The main items of interest in this demo are: |
| 24 |
|
| 25 |
- The: kind = 'popup' trait set in the PersonHandler View which marks the view |
| 26 |
as being a popup view. |
| 27 |
- The parent = info.gender.control value passed to the edit_traits method |
| 28 |
when the popup dialog is created in the object_gender_changed method. This |
| 29 |
value specifies the control that the popup dialog should be positioned near. |
| 30 |
|
| 31 |
Notes: |
| 32 |
|
| 33 |
- This demo only works on the wx backend. |
| 34 |
- Traits UI will automatically position the popup dialog near the specified |
| 35 |
control in such a way that the pop-up dialog will not overlay the control |
| 36 |
and will be entirely on the screen (as long as these two conditions do not |
| 37 |
conflict). |
| 38 |
""" |
| 39 |
|
| 40 |
# -- Imports -------------------------------------------------------------- |
| 41 |
|
| 42 |
from traits.api import HasPrivateTraits, Str, Int, Enum, Instance, Button |
| 43 |
|
| 44 |
from traitsui.api import View, HGroup, Item, Handler, UIInfo, spring |
| 45 |
|
| 46 |
# -- The PersonHandler class ---------------------------------------------- |
| 47 |
|
| 48 |
|
| 49 |
class PersonHandler(Handler): |
| 50 |
|
| 51 |
# The UIInfo object associated with the view: |
| 52 |
info = Instance(UIInfo) |
| 53 |
|
| 54 |
# The cancel button used to revert an unintentional gender change: |
| 55 |
cancel = Button('Cancel') |
| 56 |
|
| 57 |
# The pop-up customization view: |
| 58 |
view = View( |
| 59 |
HGroup( |
| 60 |
spring, |
| 61 |
Item('cancel', show_label=False), |
| 62 |
), |
| 63 |
kind='popup', |
| 64 |
) |
| 65 |
|
| 66 |
# Event handlers: |
| 67 |
def object_gender_changed(self, info): |
| 68 |
if info.initialized: |
| 69 |
self.info = info |
| 70 |
self._ui = self.edit_traits(parent=info.gender.control) |
| 71 |
|
| 72 |
def _cancel_changed(self): |
| 73 |
object = self.info.object |
| 74 |
object.gender = ['Male', 'Female'][object.gender == 'Male'] |
| 75 |
self._ui.dispose() |
| 76 |
|
| 77 |
|
| 78 |
# -- The Person class ----------------------------------------------------- |
| 79 |
|
| 80 |
|
| 81 |
class Person(HasPrivateTraits): |
| 82 |
|
| 83 |
# The person's name, age and gender: |
| 84 |
name = Str() |
| 85 |
age = Int() |
| 86 |
gender = Enum('Male', 'Female') |
| 87 |
|
| 88 |
# The traits UI view: |
| 89 |
traits_view = View( |
| 90 |
Item('name'), |
| 91 |
Item('age'), |
| 92 |
Item('gender'), |
| 93 |
title='Button Popup Demo', |
| 94 |
handler=PersonHandler, |
| 95 |
) |
| 96 |
|
| 97 |
|
| 98 |
# -- Create and run the demo ---------------------------------------------- |
| 99 |
|
| 100 |
# Create the demo: |
| 101 |
demo = Person(name='Mike Thomas', age=32) |
| 102 |
|
| 103 |
# Run the demo (if invoked from the command line): |
| 104 |
if __name__ == '__main__': |
| 105 |
demo.configure_traits() |