~ubuntu-branches/ubuntu/utopic/python-traitsui/utopic

« back to all changes in this revision

Viewing changes to examples/tutorials/doc_examples/examples/key_bindings.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#  Copyright (c) 2007, Enthought, Inc.
 
2
#  License: BSD Style.
 
3
 
 
4
# key_bindings.py -- Example of a code editor with a key bindings editor
 
5
 
 
6
#--[Imports]--------------------------------------------------------------------
 
7
from traits.api \
 
8
    import Button, Code, HasPrivateTraits, Str
 
9
 
 
10
from traitsui.api \
 
11
    import View, Item, Group, Handler, CodeEditor
 
12
 
 
13
from traitsui.key_bindings \
 
14
    import KeyBinding, KeyBindings
 
15
 
 
16
#--[Code]-----------------------------------------------------------------------
 
17
 
 
18
key_bindings = KeyBindings(
 
19
    KeyBinding( binding1    = 'Ctrl-s',
 
20
                description = 'Save to a file',
 
21
                method_name = 'save_file' ),
 
22
    KeyBinding( binding1    = 'Ctrl-r',
 
23
                description = 'Run script',
 
24
                method_name = 'run_script' ),
 
25
    KeyBinding( binding1    = 'Ctrl-k',
 
26
                description = 'Edit key bindings',
 
27
                method_name = 'edit_bindings' )
 
28
)
 
29
 
 
30
# Traits UI Handler class for bound methods
 
31
class CodeHandler ( Handler ):
 
32
 
 
33
    def save_file ( self, info ):
 
34
        info.object.status = "save file"
 
35
 
 
36
    def run_script ( self, info ):
 
37
        info.object.status = "run script"
 
38
 
 
39
    def edit_bindings ( self, info ):
 
40
        info.object.status = "edit bindings"
 
41
        key_bindings.edit_traits()
 
42
 
 
43
class KBCodeExample ( HasPrivateTraits ):
 
44
 
 
45
    code   = Code
 
46
    status = Str
 
47
    kb    = Button(label='Edit Key Bindings')
 
48
 
 
49
    view = View( Group (
 
50
                 Item( 'code',
 
51
                       style     = 'custom',
 
52
                       resizable = True ),
 
53
                 Item('status', style='readonly'),
 
54
                 'kb',
 
55
                 orientation = 'vertical',
 
56
                 show_labels = False,
 
57
                 ),
 
58
               id = 'KBCodeExample',
 
59
               key_bindings = key_bindings,
 
60
               title = 'Code Editor With Key Bindings',
 
61
               resizable = True,
 
62
 
 
63
               handler   = CodeHandler() )
 
64
 
 
65
    def _kb_fired( self, event ):
 
66
        key_bindings.edit_traits()
 
67
 
 
68
 
 
69
if __name__ == '__main__':
 
70
    KBCodeExample().configure_traits()
 
71