3 class WidgetRedirector
:
5 """Support for redirecting arbitrary widget subcommands.
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.
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.
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
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
26 def __init__(self
, widget
):
28 self
.widget
= widget
# widget instance
29 self
.tk
= tk
= widget
.tk
# widget's root
30 w
= widget
._w
# widget's (full) Tk pathname
31 self
.orig
= w
+ "_orig"
32 # Rename the Tcl command within Tcl:
33 tk
.call("rename", w
, self
.orig
)
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:
36 tk
.createcommand(w
, self
.dispatch
)
39 return "WidgetRedirector(%s<%s>)" % (self
.widget
.__class
__.__name
__,
43 for operation
in list(self
._operations
):
44 self
.unregister(operation
)
45 widget
= self
.widget
; del self
.widget
46 orig
= self
.orig
; del self
.orig
50 # restore the original widget Tcl command:
51 tk
.call("rename", orig
, w
)
53 def register(self
, operation
, function
):
54 self
._operations
[operation
] = function
55 setattr(self
.widget
, operation
, function
)
56 return OriginalCommand(self
, operation
)
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
)
68 def dispatch(self
, operation
, *args
):
69 '''Callback from Tcl which runs when the widget is referenced.
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.
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.
80 m
= self
._operations
.get(operation
)
85 return self
.tk
.call((self
.orig
, operation
) + args
)
90 class OriginalCommand
:
92 def __init__(self
, redir
, operation
):
94 self
.operation
= operation
96 self
.orig
= redir
.orig
97 self
.tk_call
= self
.tk
.call
98 self
.orig_and_operation
= (self
.orig
, self
.operation
)
101 return "OriginalCommand(%r, %r)" % (self
.redir
, self
.operation
)
103 def __call__(self
, *args
):
104 return self
.tk_call(self
.orig_and_operation
+ args
)
109 root
.wm_protocol("WM_DELETE_WINDOW", root
.quit
)
113 redir
= WidgetRedirector(text
)
114 global previous_tcl_fcn
115 def my_insert(*args
):
117 previous_tcl_fcn(*args
)
118 previous_tcl_fcn
= redir
.register("insert", my_insert
)
120 redir
.unregister("insert") # runs after first 'close window'
125 if __name__
== "__main__":