Back to index

python3.2  3.2.2
WidgetRedirector.py
Go to the documentation of this file.
00001 from tkinter import *
00002 
00003 class WidgetRedirector:
00004 
00005     """Support for redirecting arbitrary widget subcommands.
00006 
00007     Some Tk operations don't normally pass through Tkinter.  For example, if a
00008     character is inserted into a Text widget by pressing a key, a default Tk
00009     binding to the widget's 'insert' operation is activated, and the Tk library
00010     processes the insert without calling back into Tkinter.
00011 
00012     Although a binding to <Key> could be made via Tkinter, what we really want
00013     to do is to hook the Tk 'insert' operation itself.
00014 
00015     When a widget is instantiated, a Tcl command is created whose name is the
00016     same as the pathname widget._w.  This command is used to invoke the various
00017     widget operations, e.g. insert (for a Text widget). We are going to hook
00018     this command and provide a facility ('register') to intercept the widget
00019     operation.
00020 
00021     In IDLE, the function being registered provides access to the top of a
00022     Percolator chain.  At the bottom of the chain is a call to the original
00023     Tk widget operation.
00024 
00025     """
00026     def __init__(self, widget):
00027         self._operations = {}
00028         self.widget = widget            # widget instance
00029         self.tk = tk = widget.tk        # widget's root
00030         w = widget._w                   # widget's (full) Tk pathname
00031         self.orig = w + "_orig"
00032         # Rename the Tcl command within Tcl:
00033         tk.call("rename", w, self.orig)
00034         # Create a new Tcl command whose name is the widget's pathname, and
00035         # whose action is to dispatch on the operation passed to the widget:
00036         tk.createcommand(w, self.dispatch)
00037 
00038     def __repr__(self):
00039         return "WidgetRedirector(%s<%s>)" % (self.widget.__class__.__name__,
00040                                              self.widget._w)
00041 
00042     def close(self):
00043         for operation in list(self._operations):
00044             self.unregister(operation)
00045         widget = self.widget; del self.widget
00046         orig = self.orig; del self.orig
00047         tk = widget.tk
00048         w = widget._w
00049         tk.deletecommand(w)
00050         # restore the original widget Tcl command:
00051         tk.call("rename", orig, w)
00052 
00053     def register(self, operation, function):
00054         self._operations[operation] = function
00055         setattr(self.widget, operation, function)
00056         return OriginalCommand(self, operation)
00057 
00058     def unregister(self, operation):
00059         if operation in self._operations:
00060             function = self._operations[operation]
00061             del self._operations[operation]
00062             if hasattr(self.widget, operation):
00063                 delattr(self.widget, operation)
00064             return function
00065         else:
00066             return None
00067 
00068     def dispatch(self, operation, *args):
00069         '''Callback from Tcl which runs when the widget is referenced.
00070 
00071         If an operation has been registered in self._operations, apply the
00072         associated function to the args passed into Tcl. Otherwise, pass the
00073         operation through to Tk via the original Tcl function.
00074 
00075         Note that if a registered function is called, the operation is not
00076         passed through to Tk.  Apply the function returned by self.register()
00077         to *args to accomplish that.  For an example, see ColorDelegator.py.
00078 
00079         '''
00080         m = self._operations.get(operation)
00081         try:
00082             if m:
00083                 return m(*args)
00084             else:
00085                 return self.tk.call((self.orig, operation) + args)
00086         except TclError:
00087             return ""
00088 
00089 
00090 class OriginalCommand:
00091 
00092     def __init__(self, redir, operation):
00093         self.redir = redir
00094         self.operation = operation
00095         self.tk = redir.tk
00096         self.orig = redir.orig
00097         self.tk_call = self.tk.call
00098         self.orig_and_operation = (self.orig, self.operation)
00099 
00100     def __repr__(self):
00101         return "OriginalCommand(%r, %r)" % (self.redir, self.operation)
00102 
00103     def __call__(self, *args):
00104         return self.tk_call(self.orig_and_operation + args)
00105 
00106 
00107 def main():
00108     root = Tk()
00109     root.wm_protocol("WM_DELETE_WINDOW", root.quit)
00110     text = Text()
00111     text.pack()
00112     text.focus_set()
00113     redir = WidgetRedirector(text)
00114     global previous_tcl_fcn
00115     def my_insert(*args):
00116         print("insert", args)
00117         previous_tcl_fcn(*args)
00118     previous_tcl_fcn = redir.register("insert", my_insert)
00119     root.mainloop()
00120     redir.unregister("insert")  # runs after first 'close window'
00121     redir.close()
00122     root.mainloop()
00123     root.destroy()
00124 
00125 if __name__ == "__main__":
00126     main()