1 | n/a | from tkinter import * |
---|
2 | n/a | |
---|
3 | n/a | |
---|
4 | n/a | class WindowList: |
---|
5 | n/a | |
---|
6 | n/a | def __init__(self): |
---|
7 | n/a | self.dict = {} |
---|
8 | n/a | self.callbacks = [] |
---|
9 | n/a | |
---|
10 | n/a | def add(self, window): |
---|
11 | n/a | window.after_idle(self.call_callbacks) |
---|
12 | n/a | self.dict[str(window)] = window |
---|
13 | n/a | |
---|
14 | n/a | def delete(self, window): |
---|
15 | n/a | try: |
---|
16 | n/a | del self.dict[str(window)] |
---|
17 | n/a | except KeyError: |
---|
18 | n/a | # Sometimes, destroy() is called twice |
---|
19 | n/a | pass |
---|
20 | n/a | self.call_callbacks() |
---|
21 | n/a | |
---|
22 | n/a | def add_windows_to_menu(self, menu): |
---|
23 | n/a | list = [] |
---|
24 | n/a | for key in self.dict: |
---|
25 | n/a | window = self.dict[key] |
---|
26 | n/a | try: |
---|
27 | n/a | title = window.get_title() |
---|
28 | n/a | except TclError: |
---|
29 | n/a | continue |
---|
30 | n/a | list.append((title, key, window)) |
---|
31 | n/a | list.sort() |
---|
32 | n/a | for title, key, window in list: |
---|
33 | n/a | menu.add_command(label=title, command=window.wakeup) |
---|
34 | n/a | |
---|
35 | n/a | def register_callback(self, callback): |
---|
36 | n/a | self.callbacks.append(callback) |
---|
37 | n/a | |
---|
38 | n/a | def unregister_callback(self, callback): |
---|
39 | n/a | try: |
---|
40 | n/a | self.callbacks.remove(callback) |
---|
41 | n/a | except ValueError: |
---|
42 | n/a | pass |
---|
43 | n/a | |
---|
44 | n/a | def call_callbacks(self): |
---|
45 | n/a | for callback in self.callbacks: |
---|
46 | n/a | try: |
---|
47 | n/a | callback() |
---|
48 | n/a | except: |
---|
49 | n/a | t, v, tb = sys.exc_info() |
---|
50 | n/a | print("warning: callback failed in WindowList", t, ":", v) |
---|
51 | n/a | |
---|
52 | n/a | |
---|
53 | n/a | registry = WindowList() |
---|
54 | n/a | |
---|
55 | n/a | add_windows_to_menu = registry.add_windows_to_menu |
---|
56 | n/a | register_callback = registry.register_callback |
---|
57 | n/a | unregister_callback = registry.unregister_callback |
---|
58 | n/a | |
---|
59 | n/a | |
---|
60 | n/a | class ListedToplevel(Toplevel): |
---|
61 | n/a | |
---|
62 | n/a | def __init__(self, master, **kw): |
---|
63 | n/a | Toplevel.__init__(self, master, kw) |
---|
64 | n/a | registry.add(self) |
---|
65 | n/a | self.focused_widget = self |
---|
66 | n/a | |
---|
67 | n/a | def destroy(self): |
---|
68 | n/a | registry.delete(self) |
---|
69 | n/a | Toplevel.destroy(self) |
---|
70 | n/a | # If this is Idle's last window then quit the mainloop |
---|
71 | n/a | # (Needed for clean exit on Windows 98) |
---|
72 | n/a | if not registry.dict: |
---|
73 | n/a | self.quit() |
---|
74 | n/a | |
---|
75 | n/a | def update_windowlist_registry(self, window): |
---|
76 | n/a | registry.call_callbacks() |
---|
77 | n/a | |
---|
78 | n/a | def get_title(self): |
---|
79 | n/a | # Subclass can override |
---|
80 | n/a | return self.wm_title() |
---|
81 | n/a | |
---|
82 | n/a | def wakeup(self): |
---|
83 | n/a | try: |
---|
84 | n/a | if self.wm_state() == "iconic": |
---|
85 | n/a | self.wm_withdraw() |
---|
86 | n/a | self.wm_deiconify() |
---|
87 | n/a | self.tkraise() |
---|
88 | n/a | self.focused_widget.focus_set() |
---|
89 | n/a | except TclError: |
---|
90 | n/a | # This can happen when the window menu was torn off. |
---|
91 | n/a | # Simply ignore it. |
---|
92 | n/a | pass |
---|