1
# Copyright (c) 2007, Enthought, Inc.
5
Demonstrates using a popup view within another view.
7
Try changing the gender of the person from 'Male' to 'Female' using the
8
drop-down list. When the person's gender is changed, a pop-up dialog is
9
displayed immediately below the gender field providing you with the
10
opportunity to cancel the gender change.
12
If you click the Cancel button, the person's gender will return to its previous
13
value. If you click anywhere else outside of the pop-up dialog, the pop-up
14
dialog will simply disappear, leaving the person's new gender value as is.
16
The main items of interest in this demo are:
17
- The: kind = 'popup' trait set in the PersonHandler View which marks the view
18
as being a popup view.
19
- The parent = info.gender.control value passed to the edit_traits method
20
when the popup dialog is created in the object_gender_changed method. This
21
value specifies the control that the popup dialog should be positioned near.
24
- Traits UI will automatically position the popup dialog near the specified
25
control in such a way that the pop-up dialog will not overlay the control
26
and will be entirely on the screen (as long as these two conditions do not
30
#-- Imports --------------------------------------------------------------------
33
import HasPrivateTraits, Str, Int, Enum, Instance, Button
36
import View, HGroup, Item, Handler, UIInfo, spring
38
#-- The PersonHandler class ----------------------------------------------------
40
class PersonHandler ( Handler ):
42
# The UIInfo object associated with the view:
43
info = Instance( UIInfo )
45
# The cancel button used to revert an unintentional gender change:
46
cancel = Button( 'Cancel' )
48
# The pop-up customization view:
52
Item( 'cancel', show_label = False ),
58
def object_gender_changed ( self, info ):
61
self._ui = self.edit_traits( parent = info.gender.control )
63
def _cancel_changed ( self ):
64
object = self.info.object
65
object.gender = [ 'Male', 'Female' ][ object.gender == 'Male' ]
68
#-- The Person class -----------------------------------------------------------
70
class Person ( HasPrivateTraits ):
72
# The person's name, age and gender:
75
gender = Enum( 'Male', 'Female' )
82
title = 'Button Popup Demo',
83
handler = PersonHandler
86
#-- Create and run the demo ----------------------------------------------------
89
demo = Person( name = 'Mike Thomas', age = 32 )
91
# Run the demo (if invoked from the command line):
92
if __name__ == '__main__':
93
demo.configure_traits()