<s> """<STR_LIT>""" <EOL> import Tkinter <EOL> def dnd_start ( source , event ) : <EOL> h = DndHandler ( source , event ) <EOL> if h . root : <EOL> return h <EOL> else : <EOL> return None <EOL> class DndHandler : <EOL> root = None <EOL> def __init__ ( self , source , event ) : <EOL> if event . num > <NUM_LIT:5> : <EOL> return <EOL> root = event . widget . _root ( ) <EOL> try : <EOL> root . __dnd <EOL> return <EOL> except AttributeError : <EOL> root . __dnd = self <EOL> self . root = root <EOL> self . source = source <EOL> self . target = None <EOL> self . initial_button = button = event . num <EOL> self . initial_widget = widget = event . widget <EOL> self . release_pattern = "<STR_LIT>" % ( button , button ) <EOL> self . save_cursor = widget [ '<STR_LIT>' ] or "<STR_LIT>" <EOL> widget . bind ( self . release_pattern , self . on_release ) <EOL> widget . bind ( "<STR_LIT>" , self . on_motion ) <EOL> widget [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> def __del__ ( self ) : <EOL> root = self . root <EOL> self . root = None <EOL> if root : <EOL> try : <EOL> del root . __dnd <EOL> except AttributeError : <EOL> pass <EOL> def on_motion ( self , event ) : <EOL> x , y = event . x_root , event . y_root <EOL> target_widget = self . initial_widget . winfo_containing ( x , y ) <EOL> source = self . source <EOL> new_target = None <EOL> while target_widget : <EOL> try : <EOL> attr = target_widget . dnd_accept <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> new_target = attr ( source , event ) <EOL> if new_target : <EOL> break <EOL> target_widget = target_widget . master <EOL> old_target = self . target <EOL> if old_target is new_target : <EOL> if old_target : <EOL> old_target . dnd_motion ( source , event ) <EOL> else : <EOL> if old_target : <EOL> self . target = None <EOL> old_target . dnd_leave ( source , event ) <EOL> if new_target : <EOL> new_target . dnd_enter ( source , event ) <EOL> self . target = new_target <EOL> def on_release ( self , event ) : <EOL> self . finish ( event , <NUM_LIT:1> ) <EOL> def cancel ( self , event = None ) : <EOL> self . finish ( event , <NUM_LIT:0> ) <EOL> def finish ( self , event , commit = <NUM_LIT:0> ) : <EOL> target = self . target <EOL> source = self . source <EOL> widget = self . initial_widget <EOL> root = self . root <EOL> try : <EOL> del root . __dnd <EOL> self . initial_widget . unbind ( self . release_pattern ) <EOL> self . initial_widget . unbind ( "<STR_LIT>" ) <EOL> widget [ '<STR_LIT>' ] = self . save_cursor <EOL> self . target = self . source = self . initial_widget = self . root = None <EOL> if target : <EOL> if commit : <EOL> target . dnd_commit ( source , event ) <EOL> else : <EOL> target . dnd_leave ( source , event ) <EOL> finally : <EOL> source . dnd_end ( target , event ) <EOL> class Icon : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> self . canvas = self . label = self . id = None <EOL> def attach ( self , canvas , x = <NUM_LIT:10> , y = <NUM_LIT:10> ) : <EOL> if canvas is self . canvas : <EOL> self . canvas . coords ( self . id , x , y ) <EOL> return <EOL> if self . canvas : <EOL> self . detach ( ) <EOL> if not canvas : <EOL> return <EOL> label = Tkinter . Label ( canvas , text = self . name , <EOL> borderwidth = <NUM_LIT:2> , relief = "<STR_LIT>" ) <EOL> id = canvas . create_window ( x , y , window = label , anchor = "<STR_LIT>" ) <EOL> self . canvas = canvas <EOL> self . label = label <EOL> self . id = id <EOL> label . bind ( "<STR_LIT>" , self . press ) <EOL> def detach ( self ) : <EOL> canvas = self . canvas <EOL> if not canvas : <EOL> return <EOL> id = self . id <EOL> label = self . label <EOL> self . canvas = self . label = self . id = None <EOL> canvas . delete ( id ) <EOL> label . destroy ( ) <EOL> def press ( self , event ) : <EOL> if dnd_start ( self , event ) : <EOL> self . x_off = event . x <EOL> self . y_off = event . y <EOL> self . x_orig , self . y_orig = self . canvas . coords ( self . id ) <EOL> def move ( self , event ) : <EOL> x , y = self . where ( self . canvas , event ) <EOL> self . canvas . coords ( self . id , x , y ) <EOL> def putback ( self ) : <EOL> self . canvas . coords ( self . id , self . x_orig , self . y_orig ) <EOL> def where ( self , canvas , event ) : <EOL> x_org = canvas . winfo_rootx ( ) <EOL> y_org = canvas . winfo_rooty ( ) <EOL> x = event . x_root - x_org <EOL> y = event . y_root - y_org <EOL> return x - self . x_off , y - self . y_off <EOL> def dnd_end ( self , target , event ) : <EOL> pass <EOL> class Tester : <EOL> def __init__ ( self , root ) : <EOL> self . top = Tkinter . Toplevel ( root ) <EOL> self . canvas = Tkinter . Canvas ( self . top , width = <NUM_LIT:100> , height = <NUM_LIT:100> ) <EOL> self . canvas . pack ( fill = "<STR_LIT>" , expand = <NUM_LIT:1> ) <EOL> self . canvas . dnd_accept = self . dnd_accept <EOL> def dnd_accept ( self , source , event ) : <EOL> return self <EOL> def dnd_enter ( self , source , event ) : <EOL> self . canvas . focus_set ( ) <EOL> x , y = source . where ( self . canvas , event ) <EOL> x1 , y1 , x2 , y2 = source . canvas . bbox ( source . id ) <EOL> dx , dy = x2 - x1 , y2 - y1 <EOL> self . dndid = self . canvas . create_rectangle ( x , y , x + dx , y + dy ) <EOL> self . dnd_motion ( source , event ) <EOL> def dnd_motion ( self , source , event ) : <EOL> x , y = source . where ( self . canvas , event ) <EOL> x1 , y1 , x2 , y2 = self . canvas . bbox ( self . dndid ) <EOL> self . canvas . move ( self . dndid , x - x1 , y - y1 ) <EOL> def dnd_leave ( self , source , event ) : <EOL> self . top . focus_set ( ) <EOL> self . canvas . delete ( self . dndid ) <EOL> self . dndid = None <EOL> def dnd_commit ( self , source , event ) : <EOL> self . dnd_leave ( source , event ) <EOL> x , y = source . where ( self . canvas , event ) <EOL> source . attach ( self . canvas , x , y ) <EOL> def test ( ) : <EOL> root = Tkinter . Tk ( ) <EOL> root . geometry ( "<STR_LIT>" ) <EOL> Tkinter . Button ( command = root . quit , text = "<STR_LIT>" ) . pack ( ) <EOL> t1 = Tester ( root ) <EOL> t1 . top . geometry ( "<STR_LIT>" ) <EOL> t2 = Tester ( root ) <EOL> t2 . top . geometry ( "<STR_LIT>" ) <EOL> t3 = Tester ( root ) <EOL> t3 . top . geometry ( "<STR_LIT>" ) <EOL> i1 = Icon ( "<STR_LIT>" ) <EOL> i2 = Icon ( "<STR_LIT>" ) <EOL> i3 = Icon ( "<STR_LIT>" ) <EOL> i1 . attach ( t1 . canvas ) <EOL> i2 . attach ( t2 . canvas ) <EOL> i3 . attach ( t3 . canvas ) <EOL> root . mainloop ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test ( ) </s>
