<s> from tkinter import * <EOL> class WindowList : <EOL> def __init__ ( self ) : <EOL> self . dict = { } <EOL> self . callbacks = [ ] <EOL> def add ( self , window ) : <EOL> window . after_idle ( self . call_callbacks ) <EOL> self . dict [ str ( window ) ] = window <EOL> def delete ( self , window ) : <EOL> try : <EOL> del self . dict [ str ( window ) ] <EOL> except KeyError : <EOL> pass <EOL> self . call_callbacks ( ) <EOL> def add_windows_to_menu ( self , menu ) : <EOL> list = [ ] <EOL> for key in self . dict : <EOL> window = self . dict [ key ] <EOL> try : <EOL> title = window . get_title ( ) <EOL> except TclError : <EOL> continue <EOL> list . append ( ( title , key , window ) ) <EOL> list . sort ( ) <EOL> for title , key , window in list : <EOL> menu . add_command ( label = title , command = window . wakeup ) <EOL> def register_callback ( self , callback ) : <EOL> self . callbacks . append ( callback ) <EOL> def unregister_callback ( self , callback ) : <EOL> try : <EOL> self . callbacks . remove ( callback ) <EOL> except ValueError : <EOL> pass <EOL> def call_callbacks ( self ) : <EOL> for callback in self . callbacks : <EOL> try : <EOL> callback ( ) <EOL> except : <EOL> t , v , tb = sys . exc_info ( ) <EOL> print ( "<STR_LIT>" , t , "<STR_LIT::>" , v ) <EOL> registry = WindowList ( ) <EOL> add_windows_to_menu = registry . add_windows_to_menu <EOL> register_callback = registry . register_callback <EOL> unregister_callback = registry . unregister_callback <EOL> class ListedToplevel ( Toplevel ) : <EOL> def __init__ ( self , master , ** kw ) : <EOL> Toplevel . __init__ ( self , master , kw ) <EOL> registry . add ( self ) <EOL> self . focused_widget = self <EOL> def destroy ( self ) : <EOL> registry . delete ( self ) <EOL> Toplevel . destroy ( self ) <EOL> if not registry . dict : <EOL> self . quit ( ) <EOL> def update_windowlist_registry ( self , window ) : <EOL> registry . call_callbacks ( ) <EOL> def get_title ( self ) : <EOL> return self . wm_title ( ) <EOL> def wakeup ( self , anystate = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if anystate or self . wm_state ( ) == "<STR_LIT>" : <EOL> self . wm_withdraw ( ) <EOL> self . wm_deiconify ( ) <EOL> self . tkraise ( ) <EOL> self . focused_widget . focus_set ( ) <EOL> except TclError : <EOL> pass </s>
