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