관리-도구
편집 파일: WidgetRedirector.pyc
� {fc @ s� d d l m Z d d l m Z d d d � � YZ d d d � � YZ d � Z e d k r� d d l Z e j d d d d e �d d l m Z e e � n d S( i����( t print_function( t TclErrort WidgetRedirectorc B sD e Z d Z d � Z d � Z d � Z d � Z d � Z d � Z RS( s� 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. For one thing, we want a text.insert call in idle code to have the same effect as a key press. 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. We will also intercept method calls on the Tkinter class instance that represents the tk widget. 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. c C se i | _ | | _ | j | _ } | j } | d | _ | j d | | j � | j | | j � d S( s� 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. t _origt renameN( t _operationst widgett tkt _wt origt callt createcommandt dispatch( t selfR R t w( ( s0 /usr/lib64/python2.7/idlelib/WidgetRedirector.pyt __init__ s c C s d | j j j | j j f S( Ns WidgetRedirector(%s<%s>)( R t __class__t __name__R ( R ( ( s0 /usr/lib64/python2.7/idlelib/WidgetRedirector.pyt __repr__2 s c C su x$ t | j � D] } | j | � q W| j } | j } | j } | j | � | j d | j | � | ` | ` d S( sB Unregister operations and revert redirection created by .__init__.R N( t listR t unregisterR R R t deletecommandR R ( R t operationR R R ( ( s0 /usr/lib64/python2.7/idlelib/WidgetRedirector.pyt close6 s c C s- | | j | <t | j | | � t | | � S( s� Return OriginalCommand(operation) after registering function. Registration adds an operation: function pair to ._operations. It also adds a 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. ( R t setattrR t OriginalCommand( R R t function( ( s0 /usr/lib64/python2.7/idlelib/WidgetRedirector.pyt registerC s c C sZ | | j k rR | j | } | j | =y t | j | � Wn t k rM n X| Sd Sd S( s~ Return the function for the operation, or None. Deleting the instance attribute unmasks the class attribute. N( R t delattrR t AttributeErrort None( R R R ( ( s0 /usr/lib64/python2.7/idlelib/WidgetRedirector.pyR R s c G s\ | j j | � } y1 | r% | | � S| j j | j | f | � SWn t k rW d SXd S( s 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. t N( R t getR R R R ( R R t argst m( ( s0 /usr/lib64/python2.7/idlelib/WidgetRedirector.pyR b s ! ( R t __module__t __doc__R R R R R R ( ( ( s0 /usr/lib64/python2.7/idlelib/WidgetRedirector.pyR s R c B s) e Z d Z d � Z d � Z d � Z RS( s7 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) c C sO | | _ | | _ | j | _ | j | _ | j j | _ | j | f | _ d S( s� 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). N( t redirR R R R t tk_callt orig_and_operation( R R% R ( ( s0 /usr/lib64/python2.7/idlelib/WidgetRedirector.pyR � s c C s d | j | j f S( Ns OriginalCommand(%r, %r)( R% R ( R ( ( s0 /usr/lib64/python2.7/idlelib/WidgetRedirector.pyR � s c G s | j | j | � S( N( R&