Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Attributes
idlelib.WidgetRedirector.WidgetRedirector Class Reference

List of all members.

Public Member Functions

def __init__
def __repr__
def close
def register
def unregister
def dispatch

Public Attributes

 widget
 tk
 orig

Private Attributes

 _operations

Detailed Description

Support for redirecting arbitrary widget subcommands.

Some Tk operations don't normally pass through Tkinter.  For example, if a
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
processes the insert without calling back into Tkinter.

Although a binding to <Key> could be made via Tkinter, what we really want
to do is to hook the Tk 'insert' operation itself.

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
operation.

In IDLE, 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.

Definition at line 3 of file WidgetRedirector.py.


Constructor & Destructor Documentation

Definition at line 26 of file WidgetRedirector.py.

00026 
00027     def __init__(self, widget):
00028         self._operations = {}
00029         self.widget = widget            # widget instance
00030         self.tk = tk = widget.tk        # widget's root
00031         w = widget._w                   # widget's (full) Tk pathname
00032         self.orig = w + "_orig"
00033         # Rename the Tcl command within Tcl:
00034         tk.call("rename", w, self.orig)
00035         # Create a new Tcl command whose name is the widget's pathname, and
00036         # whose action is to dispatch on the operation passed to the widget:
00037         tk.createcommand(w, self.dispatch)

Here is the caller graph for this function:


Member Function Documentation

Definition at line 38 of file WidgetRedirector.py.

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

Definition at line 42 of file WidgetRedirector.py.

00042 
00043     def close(self):
00044         for operation in list(self._operations):
00045             self.unregister(operation)
00046         widget = self.widget; del self.widget
00047         orig = self.orig; del self.orig
00048         tk = widget.tk
00049         w = widget._w
00050         tk.deletecommand(w)
00051         # restore the original widget Tcl command:
00052         tk.call("rename", orig, w)

Here is the call graph for this function:

Here is the caller graph for this function:

def idlelib.WidgetRedirector.WidgetRedirector.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.

Definition at line 68 of file WidgetRedirector.py.

00068 
00069     def dispatch(self, operation, *args):
00070         '''Callback from Tcl which runs when the widget is referenced.
00071 
00072         If an operation has been registered in self._operations, apply the
00073         associated function to the args passed into Tcl. Otherwise, pass the
00074         operation through to Tk via the original Tcl function.
00075 
00076         Note that if a registered function is called, the operation is not
00077         passed through to Tk.  Apply the function returned by self.register()
00078         to *args to accomplish that.  For an example, see ColorDelegator.py.
00079 
00080         '''
00081         m = self._operations.get(operation)
00082         try:
00083             if m:
00084                 return m(*args)
00085             else:
00086                 return self.tk.call((self.orig, operation) + args)
00087         except TclError:
00088             return ""
00089 

def idlelib.WidgetRedirector.WidgetRedirector.register (   self,
  operation,
  function 
)

Definition at line 53 of file WidgetRedirector.py.

00053 
00054     def register(self, operation, function):
00055         self._operations[operation] = function
00056         setattr(self.widget, operation, function)
00057         return OriginalCommand(self, operation)

Definition at line 58 of file WidgetRedirector.py.

00058 
00059     def unregister(self, operation):
00060         if operation in self._operations:
00061             function = self._operations[operation]
00062             del self._operations[operation]
00063             if hasattr(self.widget, operation):
00064                 delattr(self.widget, operation)
00065             return function
00066         else:
00067             return None

Here is the caller graph for this function:


Member Data Documentation

Definition at line 27 of file WidgetRedirector.py.

Definition at line 31 of file WidgetRedirector.py.

Definition at line 29 of file WidgetRedirector.py.

Definition at line 28 of file WidgetRedirector.py.


The documentation for this class was generated from the following file: