WidgetRedirector.py 6.71 KB
Newer Older
1
from tkinter import TclError
David Scherer's avatar
David Scherer committed
2 3

class WidgetRedirector:
4 5
    """Support for redirecting arbitrary widget subcommands.

6
    Some Tk operations don't normally pass through tkinter.  For example, if a
7 8
    character is inserted into a Text widget by pressing a key, a default Tk
    binding to the widget's 'insert' operation is activated, and the Tk library
9
    processes the insert without calling back into tkinter.
10

11 12 13
    Although a binding to <Key> could be made via tkinter, what we really want
    to do is to hook the Tk 'insert' operation itself.  For one thing, we want
    a text.insert call in idle code to have the same effect as a key press.
David Scherer's avatar
David Scherer committed
14

15 16 17 18
    When a widget is instantiated, a Tcl command is created whose name is the
    same as the pathname widget._w.  This command is used to invoke the various
    widget operations, e.g. insert (for a Text widget). We are going to hook
    this command and provide a facility ('register') to intercept the widget
19 20
    operation.  We will also intercept method calls on the tkinter class
    instance that represents the tk widget.
21

22 23 24 25
    In IDLE, WidgetRedirector is used in Percolator to intercept Text
    commands.  The function being registered provides access to the top
    of a Percolator chain.  At the bottom of the chain is a call to the
    original Tk widget operation.
26
    """
David Scherer's avatar
David Scherer committed
27
    def __init__(self, widget):
28 29 30 31 32 33 34 35 36 37
        '''Initialize attributes and setup redirection.

        _operations: dict mapping operation name to new function.
        widget: the widget whose tcl command is to be intercepted.
        tk: widget.tk, a convenience attribute, probably not needed.
        orig: new name of the original tcl command.

        Since renaming to orig fails with TclError when orig already
        exists, only one WidgetDirector can exist for a given widget.
        '''
38 39 40 41
        self._operations = {}
        self.widget = widget            # widget instance
        self.tk = tk = widget.tk        # widget's root
        w = widget._w                   # widget's (full) Tk pathname
David Scherer's avatar
David Scherer committed
42
        self.orig = w + "_orig"
43
        # Rename the Tcl command within Tcl:
David Scherer's avatar
David Scherer committed
44
        tk.call("rename", w, self.orig)
45 46
        # Create a new Tcl command whose name is the widget's pathname, and
        # whose action is to dispatch on the operation passed to the widget:
David Scherer's avatar
David Scherer committed
47 48 49 50 51 52 53
        tk.createcommand(w, self.dispatch)

    def __repr__(self):
        return "WidgetRedirector(%s<%s>)" % (self.widget.__class__.__name__,
                                             self.widget._w)

    def close(self):
54
        "Unregister operations and revert redirection created by .__init__."
55 56
        for operation in list(self._operations):
            self.unregister(operation)
57
        widget = self.widget
David Scherer's avatar
David Scherer committed
58 59
        tk = widget.tk
        w = widget._w
60
        # Restore the original widget Tcl command.
David Scherer's avatar
David Scherer committed
61
        tk.deletecommand(w)
62 63 64
        tk.call("rename", self.orig, w)
        del self.widget, self.tk  # Should not be needed
        # if instance is deleted after close, as in Percolator.
David Scherer's avatar
David Scherer committed
65

66
    def register(self, operation, function):
67 68
        '''Return OriginalCommand(operation) after registering function.

69 70 71 72 73 74 75
        Registration adds an operation: function pair to ._operations.
        It also adds an widget function attribute that masks the tkinter
        class instance method.  Method masking operates independently
        from command dispatch.

        If a second function is registered for the same operation, the
        first function is replaced in both places.
76
        '''
77 78 79 80 81
        self._operations[operation] = function
        setattr(self.widget, operation, function)
        return OriginalCommand(self, operation)

    def unregister(self, operation):
82 83 84 85
        '''Return the function for the operation, or None.

        Deleting the instance attribute unmasks the class attribute.
        '''
86 87 88
        if operation in self._operations:
            function = self._operations[operation]
            del self._operations[operation]
89
            try:
90
                delattr(self.widget, operation)
91 92
            except AttributeError:
                pass
David Scherer's avatar
David Scherer committed
93 94 95 96
            return function
        else:
            return None

97 98 99 100 101 102 103 104 105 106 107 108 109
    def dispatch(self, operation, *args):
        '''Callback from Tcl which runs when the widget is referenced.

        If an operation has been registered in self._operations, apply the
        associated function to the args passed into Tcl. Otherwise, pass the
        operation through to Tk via the original Tcl function.

        Note that if a registered function is called, the operation is not
        passed through to Tk.  Apply the function returned by self.register()
        to *args to accomplish that.  For an example, see ColorDelegator.py.

        '''
        m = self._operations.get(operation)
David Scherer's avatar
David Scherer committed
110 111
        try:
            if m:
112
                return m(*args)
David Scherer's avatar
David Scherer committed
113
            else:
114
                return self.tk.call((self.orig, operation) + args)
David Scherer's avatar
David Scherer committed
115 116 117 118 119
        except TclError:
            return ""


class OriginalCommand:
120 121 122 123 124 125 126 127 128
    '''Callable for original tk command that has been redirected.

    Returned by .register; can be used in the function registered.
    redir = WidgetRedirector(text)
    def my_insert(*args):
        print("insert", args)
        original_insert(*args)
    original_insert = redir.register("insert", my_insert)
    '''
David Scherer's avatar
David Scherer committed
129

130
    def __init__(self, redir, operation):
131 132 133 134 135
        '''Create .tk_call and .orig_and_operation for .__call__ method.

        .redir and .operation store the input args for __repr__.
        .tk and .orig copy attributes of .redir (probably not needed).
        '''
David Scherer's avatar
David Scherer committed
136
        self.redir = redir
137
        self.operation = operation
138 139 140 141 142
        self.tk = redir.tk  # redundant with self.redir
        self.orig = redir.orig  # redundant with self.redir
        # These two could be deleted after checking recipient code.
        self.tk_call = redir.tk.call
        self.orig_and_operation = (redir.orig, operation)
David Scherer's avatar
David Scherer committed
143 144

    def __repr__(self):
145
        return "OriginalCommand(%r, %r)" % (self.redir, self.operation)
David Scherer's avatar
David Scherer committed
146 147

    def __call__(self, *args):
148
        return self.tk_call(self.orig_and_operation + args)
David Scherer's avatar
David Scherer committed
149 150


151 152 153 154
def _widget_redirector(parent):  # htest #
    from tkinter import Tk, Text
    import re

David Scherer's avatar
David Scherer committed
155
    root = Tk()
156 157 158 159
    root.title("Test WidgetRedirector")
    width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
    root.geometry("+%d+%d"%(x, y + 150))
    text = Text(root)
David Scherer's avatar
David Scherer committed
160 161 162 163
    text.pack()
    text.focus_set()
    redir = WidgetRedirector(text)
    def my_insert(*args):
164
        print("insert", args)
165 166
        original_insert(*args)
    original_insert = redir.register("insert", my_insert)
167
    root.mainloop()
David Scherer's avatar
David Scherer committed
168 169

if __name__ == "__main__":
170
    import unittest
171 172
    unittest.main('idlelib.idle_test.test_widgetredir',
                  verbosity=2, exit=False)
173 174
    from idlelib.idle_test.htest import run
    run(_widget_redirector)