home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / WINDOWLIST.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  2.3 KB  |  86 lines

  1. from Tkinter import *
  2.  
  3. class WindowList:
  4.  
  5.     def __init__(self):
  6.         self.dict = {}
  7.         self.callbacks = []
  8.  
  9.     def add(self, window):
  10.         window.after_idle(self.call_callbacks)
  11.         self.dict[str(window)] = window
  12.  
  13.     def delete(self, window):
  14.         try:
  15.             del self.dict[str(window)]
  16.         except KeyError:
  17.             # Sometimes, destroy() is called twice
  18.             pass
  19.         self.call_callbacks()
  20.  
  21.     def add_windows_to_menu(self,  menu):
  22.         list = []
  23.         for key in self.dict.keys():
  24.             window = self.dict[key]
  25.             try:
  26.                 title = window.get_title()
  27.             except TclError:
  28.                 continue
  29.             list.append((title, window))
  30.         list.sort()
  31.         for title, window in list:
  32.             if title == "Python Shell":
  33.                 # Hack -- until we have a better way to this
  34.                 continue
  35.             menu.add_command(label=title, command=window.wakeup)
  36.  
  37.     def register_callback(self, callback):
  38.         self.callbacks.append(callback)
  39.  
  40.     def unregister_callback(self, callback):
  41.         try:
  42.            self.callbacks.remove(callback)
  43.         except ValueError:
  44.             pass
  45.  
  46.     def call_callbacks(self):
  47.         for callback in self.callbacks:
  48.             try:
  49.                 callback()
  50.             except:
  51.                 print "warning: callback failed in WindowList", \
  52.                       sys.exc_type, ":", sys.exc_value
  53.  
  54. registry = WindowList()
  55.  
  56. add_windows_to_menu = registry.add_windows_to_menu
  57. register_callback = registry.register_callback
  58. unregister_callback = registry.unregister_callback
  59.  
  60.  
  61. class ListedToplevel(Toplevel):
  62.  
  63.     def __init__(self, master, **kw):
  64.         Toplevel.__init__(self, master, kw)
  65.         registry.add(self)
  66.  
  67.     def destroy(self):
  68.         registry.delete(self)
  69.         Toplevel.destroy(self)
  70.  
  71.     def get_title(self):
  72.         # Subclass can override
  73.         return self.wm_title()
  74.  
  75.     def wakeup(self):
  76.         try:
  77.             if self.wm_state() == "iconic":
  78.                 self.wm_deiconify()
  79.             else:
  80.                 self.tkraise()
  81.             self.focus_set()
  82.         except TclError:
  83.             # This can happen when the window menu was torn off.
  84.             # Simply ignore it.
  85.             pass
  86.