extend.txt 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. Writing an IDLE extension
  2. =========================
  3. An IDLE extension can define new key bindings and menu entries for IDLE
  4. edit windows. There is a simple mechanism to load extensions when IDLE
  5. starts up and to attach them to each edit window. (It is also possible
  6. to make other changes to IDLE, but this must be done by editing the IDLE
  7. source code.)
  8. The list of extensions loaded at startup time is configured by editing
  9. the file config-extensions.def. See below for details.
  10. An IDLE extension is defined by a class. Methods of the class define
  11. actions that are invoked by event bindings or menu entries. Class (or
  12. instance) variables define the bindings and menu additions; these are
  13. automatically applied by IDLE when the extension is linked to an edit
  14. window.
  15. An IDLE extension class is instantiated with a single argument,
  16. `editwin', an EditorWindow instance. The extension cannot assume much
  17. about this argument, but it is guaranteed to have the following instance
  18. variables:
  19. text a Text instance (a widget)
  20. io an IOBinding instance (more about this later)
  21. flist the FileList instance (shared by all edit windows)
  22. (There are a few more, but they are rarely useful.)
  23. The extension class must not directly bind Window Manager (e.g. X) events.
  24. Rather, it must define one or more virtual events, e.g. <<z-in>>, and
  25. corresponding methods, e.g. z_in_event(). The virtual events will be
  26. bound to the corresponding methods, and Window Manager events can then be bound
  27. to the virtual events. (This indirection is done so that the key bindings can
  28. easily be changed, and so that other sources of virtual events can exist, such
  29. as menu entries.)
  30. An extension can define menu entries. This is done with a class or instance
  31. variable named menudefs; it should be a list of pairs, where each pair is a
  32. menu name (lowercase) and a list of menu entries. Each menu entry is either
  33. None (to insert a separator entry) or a pair of strings (menu_label,
  34. virtual_event). Here, menu_label is the label of the menu entry, and
  35. virtual_event is the virtual event to be generated when the entry is selected.
  36. An underscore in the menu label is removed; the character following the
  37. underscore is displayed underlined, to indicate the shortcut character (for
  38. Windows).
  39. At the moment, extensions cannot define whole new menus; they must define
  40. entries in existing menus. Some menus are not present on some windows; such
  41. entry definitions are then ignored, but key bindings are still applied. (This
  42. should probably be refined in the future.)
  43. Extensions are not required to define menu entries for all the events they
  44. implement. (They are also not required to create keybindings, but in that
  45. case there must be empty bindings in cofig-extensions.def)
  46. Here is a partial example from zzdummy.py:
  47. class ZzDummy:
  48. menudefs = [
  49. ('format', [
  50. ('Z in', '<<z-in>>'),
  51. ('Z out', '<<z-out>>'),
  52. ] )
  53. ]
  54. def __init__(self, editwin):
  55. self.editwin = editwin
  56. def z_in_event(self, event=None):
  57. "...Do what you want here..."
  58. The final piece of the puzzle is the file "config-extensions.def", which is
  59. used to configure the loading of extensions and to establish key (or, more
  60. generally, event) bindings to the virtual events defined in the extensions.
  61. See the comments at the top of config-extensions.def for information. It's
  62. currently necessary to manually modify that file to change IDLE's extension
  63. loading or extension key bindings.
  64. For further information on binding refer to the Tkinter Resources web page at
  65. python.org and to the Tk Command "bind" man page.