0001 from Tkinter import * 0002 0003 class WindowList: 0004 0005 def __init__(self): 0006 self.dict = {} 0007 self.callbacks = [] 0008 0009 def add(self, window): 0010 window.after_idle(self.call_callbacks) 0011 self.dict[str(window)] = window 0012 0013 def delete(self, window): 0014 try: 0015 del self.dict[str(window)] 0016 except KeyError: 0017 # Sometimes, destroy() is called twice 0018 pass 0019 self.call_callbacks() 0020 0021 def add_windows_to_menu(self, menu): 0022 list = [] 0023 for key in self.dict.keys(): 0024 window = self.dict[key] 0025 try: 0026 title = window.get_title() 0027 except TclError: 0028 continue 0029 list.append((title, window)) 0030 list.sort() 0031 for title, window in list: 0032 menu.add_command(label=title, command=window.wakeup) 0033 0034 def register_callback(self, callback): 0035 self.callbacks.append(callback) 0036 0037 def unregister_callback(self, callback): 0038 try: 0039 self.callbacks.remove(callback) 0040 except ValueError: 0041 pass 0042 0043 def call_callbacks(self): 0044 for callback in self.callbacks: 0045 try: 0046 callback() 0047 except: 0048 print "warning: callback failed in WindowList", \ 0049 sys.exc_type, ":", sys.exc_value 0050 0051 registry = WindowList() 0052 0053 add_windows_to_menu = registry.add_windows_to_menu 0054 register_callback = registry.register_callback 0055 unregister_callback = registry.unregister_callback 0056 0057 0058 class ListedToplevel(Toplevel): 0059 0060 def __init__(self, master, **kw): 0061 Toplevel.__init__(self, master, kw) 0062 registry.add(self) 0063 self.focused_widget = self 0064 0065 def destroy(self): 0066 registry.delete(self) 0067 Toplevel.destroy(self) 0068 # If this is Idle's last window then quit the mainloop 0069 # (Needed for clean exit on Windows 98) 0070 if not registry.dict: 0071 self.quit() 0072 0073 def update_windowlist_registry(self, window): 0074 registry.call_callbacks() 0075 0076 def get_title(self): 0077 # Subclass can override 0078 return self.wm_title() 0079 0080 def wakeup(self): 0081 try: 0082 if self.wm_state() == "iconic": 0083 self.wm_withdraw() 0084 self.wm_deiconify() 0085 self.tkraise() 0086 self.focused_widget.focus_set() 0087 except TclError: 0088 # This can happen when the window menu was torn off. 0089 # Simply ignore it. 0090 pass 0091
Generated by PyXR 0.9.4