Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 1 | from tkinter import * |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 2 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 3 | class WidgetRedirector: |
| 4 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 5 | """Support for redirecting arbitrary widget subcommands. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 7 | Some Tk operations don't normally pass through Tkinter. For example, if a |
| 8 | character is inserted into a Text widget by pressing a key, a default Tk |
| 9 | binding to the widget's 'insert' operation is activated, and the Tk library |
| 10 | processes the insert without calling back into Tkinter. |
| 11 | |
| 12 | Although a binding to <Key> could be made via Tkinter, what we really want |
| 13 | to do is to hook the Tk 'insert' operation itself. |
| 14 | |
| 15 | When a widget is instantiated, a Tcl command is created whose name is the |
| 16 | same as the pathname widget._w. This command is used to invoke the various |
| 17 | widget operations, e.g. insert (for a Text widget). We are going to hook |
| 18 | this command and provide a facility ('register') to intercept the widget |
| 19 | operation. |
| 20 | |
| 21 | In IDLE, the function being registered provides access to the top of a |
| 22 | Percolator chain. At the bottom of the chain is a call to the original |
| 23 | Tk widget operation. |
| 24 | |
| 25 | """ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 26 | def __init__(self, widget): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 27 | self._operations = {} |
| 28 | self.widget = widget # widget instance |
| 29 | self.tk = tk = widget.tk # widget's root |
| 30 | w = widget._w # widget's (full) Tk pathname |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 31 | self.orig = w + "_orig" |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 32 | # Rename the Tcl command within Tcl: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 33 | tk.call("rename", w, self.orig) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 34 | # Create a new Tcl command whose name is the widget's pathname, and |
| 35 | # whose action is to dispatch on the operation passed to the widget: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 36 | tk.createcommand(w, self.dispatch) |
| 37 | |
| 38 | def __repr__(self): |
| 39 | return "WidgetRedirector(%s<%s>)" % (self.widget.__class__.__name__, |
| 40 | self.widget._w) |
| 41 | |
| 42 | def close(self): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 43 | for operation in list(self._operations): |
| 44 | self.unregister(operation) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 45 | widget = self.widget; del self.widget |
| 46 | orig = self.orig; del self.orig |
| 47 | tk = widget.tk |
| 48 | w = widget._w |
| 49 | tk.deletecommand(w) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 50 | # restore the original widget Tcl command: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 51 | tk.call("rename", orig, w) |
| 52 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 53 | def register(self, operation, function): |
| 54 | self._operations[operation] = function |
| 55 | setattr(self.widget, operation, function) |
| 56 | return OriginalCommand(self, operation) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 58 | def unregister(self, operation): |
| 59 | if operation in self._operations: |
| 60 | function = self._operations[operation] |
| 61 | del self._operations[operation] |
| 62 | if hasattr(self.widget, operation): |
| 63 | delattr(self.widget, operation) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 64 | return function |
| 65 | else: |
| 66 | return None |
| 67 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 68 | def dispatch(self, operation, *args): |
| 69 | '''Callback from Tcl which runs when the widget is referenced. |
| 70 | |
| 71 | If an operation has been registered in self._operations, apply the |
| 72 | associated function to the args passed into Tcl. Otherwise, pass the |
| 73 | operation through to Tk via the original Tcl function. |
| 74 | |
| 75 | Note that if a registered function is called, the operation is not |
| 76 | passed through to Tk. Apply the function returned by self.register() |
| 77 | to *args to accomplish that. For an example, see ColorDelegator.py. |
| 78 | |
| 79 | ''' |
| 80 | m = self._operations.get(operation) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 81 | try: |
| 82 | if m: |
Raymond Hettinger | 931237e | 2003-07-09 18:48:24 +0000 | [diff] [blame] | 83 | return m(*args) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 84 | else: |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 85 | return self.tk.call((self.orig, operation) + args) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 86 | except TclError: |
| 87 | return "" |
| 88 | |
| 89 | |
| 90 | class OriginalCommand: |
| 91 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 92 | def __init__(self, redir, operation): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 93 | self.redir = redir |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 94 | self.operation = operation |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 95 | self.tk = redir.tk |
| 96 | self.orig = redir.orig |
| 97 | self.tk_call = self.tk.call |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 98 | self.orig_and_operation = (self.orig, self.operation) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 99 | |
| 100 | def __repr__(self): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 101 | return "OriginalCommand(%r, %r)" % (self.redir, self.operation) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 102 | |
| 103 | def __call__(self, *args): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 104 | return self.tk_call(self.orig_and_operation + args) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 105 | |
| 106 | |
| 107 | def main(): |
| 108 | root = Tk() |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 109 | root.wm_protocol("WM_DELETE_WINDOW", root.quit) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 110 | text = Text() |
| 111 | text.pack() |
| 112 | text.focus_set() |
| 113 | redir = WidgetRedirector(text) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 114 | global previous_tcl_fcn |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 115 | def my_insert(*args): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 116 | print("insert", args) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 117 | previous_tcl_fcn(*args) |
| 118 | previous_tcl_fcn = redir.register("insert", my_insert) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 119 | root.mainloop() |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 120 | redir.unregister("insert") # runs after first 'close window' |
| 121 | redir.close() |
| 122 | root.mainloop() |
| 123 | root.destroy() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 124 | |
| 125 | if __name__ == "__main__": |
| 126 | main() |