| 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 |
# key_bindings.py -- Example of a code editor with a key bindings editor |
| 12 |
|
| 13 |
from traits.api import Button, Code, HasPrivateTraits, observe, Str |
| 14 |
from traitsui.api import Group, Handler, Item, View |
| 15 |
from traitsui.key_bindings import KeyBinding, KeyBindings |
| 16 |
|
| 17 |
key_bindings = KeyBindings( |
| 18 |
KeyBinding( |
| 19 |
binding1='Ctrl-s', |
| 20 |
description='Save to a file', |
| 21 |
method_name='save_file', |
| 22 |
), |
| 23 |
KeyBinding( |
| 24 |
binding1='Ctrl-r', |
| 25 |
description='Run script', |
| 26 |
method_name='run_script', |
| 27 |
), |
| 28 |
KeyBinding( |
| 29 |
binding1='Ctrl-k', |
| 30 |
description='Edit key bindings', |
| 31 |
# special method name handled internally |
| 32 |
method_name='edit_bindings', |
| 33 |
), |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
class CodeHandler(Handler): |
| 38 |
"""Handler class for bound methods.""" |
| 39 |
|
| 40 |
def save_file(self, info): |
| 41 |
info.object.status = "save file" |
| 42 |
|
| 43 |
def run_script(self, info): |
| 44 |
info.object.status = "run script" |
| 45 |
|
| 46 |
|
| 47 |
class KBCodeExample(HasPrivateTraits): |
| 48 |
|
| 49 |
code = Code() |
| 50 |
status = Str() |
| 51 |
kb = Button(label='Edit Key Bindings') |
| 52 |
|
| 53 |
view = View( |
| 54 |
Group( |
| 55 |
Item('code', style='custom', resizable=True), |
| 56 |
Item('status', style='readonly'), |
| 57 |
'kb', |
| 58 |
orientation='vertical', |
| 59 |
show_labels=False, |
| 60 |
), |
| 61 |
id='KBCodeExample', |
| 62 |
key_bindings=key_bindings, |
| 63 |
title='Code Editor With Key Bindings', |
| 64 |
resizable=True, |
| 65 |
handler=CodeHandler(), |
| 66 |
) |
| 67 |
|
| 68 |
@observe('kb') |
| 69 |
def _edit_key_bindings(self, event): |
| 70 |
key_bindings.edit() |
| 71 |
|
| 72 |
|
| 73 |
if __name__ == '__main__': |
| 74 |
KBCodeExample().configure_traits() |