ok
Direktori : /lib64/python2.7/lib-tk/ |
Current File : //lib64/python2.7/lib-tk/Tkdnd.pyc |
� zfc @ sw d Z d d l Z d � Z d d d � � YZ d d d � � YZ d d d � � YZ d � Z e d k rs e � n d S( sF Drag-and-drop support for Tkinter. This is very preliminary. I currently only support dnd *within* one application, between different windows (or within the same window). I am trying to make this as generic as possible -- not dependent on the use of a particular widget or icon type, etc. I also hope that this will work with Pmw. To enable an object to be dragged, you must create an event binding for it that starts the drag-and-drop process. Typically, you should bind <ButtonPress> to a callback function that you write. The function should call Tkdnd.dnd_start(source, event), where 'source' is the object to be dragged, and 'event' is the event that invoked the call (the argument to your callback function). Even though this is a class instantiation, the returned instance should not be stored -- it will be kept alive automatically for the duration of the drag-and-drop. When a drag-and-drop is already in process for the Tk interpreter, the call is *ignored*; this normally averts starting multiple simultaneous dnd processes, e.g. because different button callbacks all dnd_start(). The object is *not* necessarily a widget -- it can be any application-specific object that is meaningful to potential drag-and-drop targets. Potential drag-and-drop targets are discovered as follows. Whenever the mouse moves, and at the start and end of a drag-and-drop move, the Tk widget directly under the mouse is inspected. This is the target widget (not to be confused with the target object, yet to be determined). If there is no target widget, there is no dnd target object. If there is a target widget, and it has an attribute dnd_accept, this should be a function (or any callable object). The function is called as dnd_accept(source, event), where 'source' is the object being dragged (the object passed to dnd_start() above), and 'event' is the most recent event object (generally a <Motion> event; it can also be <ButtonPress> or <ButtonRelease>). If the dnd_accept() function returns something other than None, this is the new dnd target object. If dnd_accept() returns None, or if the target widget has no dnd_accept attribute, the target widget's parent is considered as the target widget, and the search for a target object is repeated from there. If necessary, the search is repeated all the way up to the root widget. If none of the target widgets can produce a target object, there is no target object (the target object is None). The target object thus produced, if any, is called the new target object. It is compared with the old target object (or None, if there was no old target widget). There are several cases ('source' is the source object, and 'event' is the most recent event object): - Both the old and new target objects are None. Nothing happens. - The old and new target objects are the same object. Its method dnd_motion(source, event) is called. - The old target object was None, and the new target object is not None. The new target object's method dnd_enter(source, event) is called. - The new target object is None, and the old target object is not None. The old target object's method dnd_leave(source, event) is called. - The old and new target objects differ and neither is None. The old target object's method dnd_leave(source, event), and then the new target object's method dnd_enter(source, event) is called. Once this is done, the new target object replaces the old one, and the Tk mainloop proceeds. The return value of the methods mentioned above is ignored; if they raise an exception, the normal exception handling mechanisms take over. The drag-and-drop processes can end in two ways: a final target object is selected, or no final target object is selected. When a final target object is selected, it will always have been notified of the potential drop by a call to its dnd_enter() method, as described above, and possibly one or more calls to its dnd_motion() method; its dnd_leave() method has not been called since the last call to dnd_enter(). The target is notified of the drop by a call to its method dnd_commit(source, event). If no final target object is selected, and there was an old target object, its dnd_leave(source, event) method is called to complete the dnd sequence. Finally, the source object is notified that the drag-and-drop process is over, by a call to source.dnd_end(target, event), specifying either the selected target object, or None if no target object was selected. The source object can use this to implement the commit action; this is sometimes simpler than to do it in the target's dnd_commit(). The target's dnd_commit() method could then simply be aliased to dnd_leave(). At any time during a dnd sequence, the application can cancel the sequence by calling the cancel() method on the object returned by dnd_start(). This will call dnd_leave() if a target is currently active; it will never call dnd_commit(). i����Nc C s$ t | | � } | j r | Sd Sd S( N( t DndHandlert roott None( t sourcet eventt h( ( s$ /usr/lib64/python2.7/lib-tk/Tkdnd.pyt dnd_startl s R c B sJ e Z d Z d � Z d � Z d � Z d � Z d d � Z d d � Z RS( c C s� | j d k r d S| j j � } y | j d SWn# t k rV | | _ | | _ n X| | _ d | _ | j | _ } | j | _ } d | | f | _ | d p� d | _ | j | j | j � | j d | j � d | d <d S( Ni s <B%d-ButtonRelease-%d>t cursort s <Motion>t hand2( t numt widgett _roott _DndHandler__dndt AttributeErrorR R R t targett initial_buttont initial_widgett release_patternt save_cursort bindt on_releaset on_motion( t selfR R R t buttonR ( ( s$ /usr/lib64/python2.7/lib-tk/Tkdnd.pyt __init__z s$ c C s= | j } d | _ | r9 y | ` Wq9 t k r5 q9 Xn d S( N( R R R R ( R R ( ( s$ /usr/lib64/python2.7/lib-tk/Tkdnd.pyt __del__� s c C s | j | j } } | j j | | � } | j } d } xM | r� y | j } Wn t k r` n X| | | � } | rz Pn | j } q: W| j } | | k r� | r� | j | | � q� nD | r� d | _ | j | | � n | r� | j | | � | | _ n d S( N( t x_roott y_rootR t winfo_containingR R t dnd_acceptR t masterR t dnd_motiont dnd_leavet dnd_enter( R R t xt yt target_widgetR t new_targett attrt old_target( ( s$ /usr/lib64/python2.7/lib-tk/Tkdnd.pyR � s. c C s | j | d � d S( Ni ( t finish( R R ( ( s$ /usr/lib64/python2.7/lib-tk/Tkdnd.pyR � s c C s | j | d � d S( Ni ( R) ( R R ( ( s$ /usr/lib64/python2.7/lib-tk/Tkdnd.pyt cancel� s i c C s� | j } | j } | j } | j } z� | ` | j j | j � | j j d � | j | d <d | _ | _ | _ | _ | r� | r� | j | | � q� | j | | � n Wd | j | | � Xd S( Ns <Motion>R ( R R R R R t unbindR R R t dnd_commitR! t dnd_end( R R t commitR R R R ( ( s$ /usr/lib64/python2.7/lib-tk/Tkdnd.pyR) � s N( t __name__t __module__R R R R R R R* R) ( ( ( s$ /usr/lib64/python2.7/lib-tk/Tkdnd.pyR v s t Iconc B sV e Z d � Z d d d � Z d � Z d � Z d � Z d � Z d � Z d � Z RS( c C s$ | | _ d | _ | _ | _ d S( N( t nameR t canvast labelt id( R R2 ( ( s$ /usr/lib64/python2.7/lib-tk/Tkdnd.pyR � s i c C s� | | j k r, | j j | j | | � d S| j rB | j � n | sL d St j | d | j d d d d �} | j | | d | d d �} | | _ | | _ | | _ | j d | j � d S( Nt textt borderwidthi t relieft raisedt windowt anchort nws <ButtonPress>( R3 t coordsR5 t detacht Tkintert LabelR2 t create_windowR4 R t press( R R3 R# R$ R4 R5 ( ( s$ /usr/lib64/python2.7/lib-tk/Tkdnd.pyt attach� s c C sW | j } | s d S| j } | j } d | _ | _ | _ | j | � | j � d S( N( R3 R5 R4 R t deletet destroy( R R3 R5 R4 ( ( s$ /usr/lib64/python2.7/lib-tk/Tkdnd.pyR>