Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 1 | from Tkinter import * |
| 2 | |
| 3 | class WindowList: |
| 4 | |
| 5 | def __init__(self): |
| 6 | self.dict = {} |
Guido van Rossum | 5437ddf | 1999-02-17 17:19:37 +0000 | [diff] [blame] | 7 | self.callbacks = [] |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 8 | |
| 9 | def add(self, window): |
Guido van Rossum | 5437ddf | 1999-02-17 17:19:37 +0000 | [diff] [blame] | 10 | window.after_idle(self.call_callbacks) |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 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 |
Guido van Rossum | be5f2f1 | 1999-02-17 17:34:25 +0000 | [diff] [blame] | 19 | self.call_callbacks() |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 20 | |
| 21 | def add_windows_to_menu(self, menu): |
| 22 | list = [] |
| 23 | for key in self.dict.keys(): |
| 24 | window = self.dict[key] |
Guido van Rossum | ec119a3 | 1999-02-08 22:27:49 +0000 | [diff] [blame] | 25 | try: |
| 26 | title = window.get_title() |
| 27 | except TclError: |
| 28 | continue |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 5437ddf | 1999-02-17 17:19:37 +0000 | [diff] [blame] | 37 | def register_callback(self, callback): |
| 38 | self.callbacks.append(callback) |
| 39 | |
| 40 | def unregister_callback(self, callback): |
| 41 | try: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 42 | self.callbacks.remove(callback) |
Guido van Rossum | 553fa44 | 2000-02-15 18:03:01 +0000 | [diff] [blame] | 43 | except ValueError: |
Guido van Rossum | 5437ddf | 1999-02-17 17:19:37 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 54 | registry = WindowList() |
| 55 | |
Guido van Rossum | 5437ddf | 1999-02-17 17:19:37 +0000 | [diff] [blame] | 56 | add_windows_to_menu = registry.add_windows_to_menu |
| 57 | register_callback = registry.register_callback |
| 58 | unregister_callback = registry.unregister_callback |
Guido van Rossum | 5437ddf | 1999-02-17 17:19:37 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 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): |
Guido van Rossum | 2403b0c | 1999-01-09 22:01:33 +0000 | [diff] [blame] | 76 | try: |
Guido van Rossum | 36911a1 | 1999-01-18 15:18:57 +0000 | [diff] [blame] | 77 | if self.wm_state() == "iconic": |
| 78 | self.wm_deiconify() |
| 79 | else: |
| 80 | self.tkraise() |
Guido van Rossum | 2403b0c | 1999-01-09 22:01:33 +0000 | [diff] [blame] | 81 | self.focus_set() |
| 82 | except TclError: |
| 83 | # This can happen when the window menu was torn off. |
| 84 | # Simply ignore it. |
| 85 | pass |