annotate Lib/lib-tk/SimpleDialog.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 5cec436d64a2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47182
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
1 """A simple but flexible modal dialog box."""
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
2
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
3
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
4 from Tkinter import *
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
5
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
6
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
7 class SimpleDialog:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
8
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
9 def __init__(self, master,
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
10 text='', buttons=[], default=None, cancel=None,
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
11 title=None, class_=None):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
12 if class_:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
13 self.root = Toplevel(master, class_=class_)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
14 else:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
15 self.root = Toplevel(master)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
16 if title:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
17 self.root.title(title)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
18 self.root.iconname(title)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
19 self.message = Message(self.root, text=text, aspect=400)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
20 self.message.pack(expand=1, fill=BOTH)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
21 self.frame = Frame(self.root)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
22 self.frame.pack()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
23 self.num = default
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
24 self.cancel = cancel
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
25 self.default = default
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
26 self.root.bind('<Return>', self.return_event)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
27 for num in range(len(buttons)):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
28 s = buttons[num]
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
29 b = Button(self.frame, text=s,
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
30 command=(lambda self=self, num=num: self.done(num)))
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
31 if num == default:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
32 b.config(relief=RIDGE, borderwidth=8)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
33 b.pack(side=LEFT, fill=BOTH, expand=1)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
34 self.root.protocol('WM_DELETE_WINDOW', self.wm_delete_window)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
35 self._set_transient(master)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
36
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
37 def _set_transient(self, master, relx=0.5, rely=0.3):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
38 widget = self.root
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
39 widget.withdraw() # Remain invisible while we figure out the geometry
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
40 widget.transient(master)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
41 widget.update_idletasks() # Actualize geometry information
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
42 if master.winfo_ismapped():
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
43 m_width = master.winfo_width()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
44 m_height = master.winfo_height()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
45 m_x = master.winfo_rootx()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
46 m_y = master.winfo_rooty()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
47 else:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
48 m_width = master.winfo_screenwidth()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
49 m_height = master.winfo_screenheight()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
50 m_x = m_y = 0
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
51 w_width = widget.winfo_reqwidth()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
52 w_height = widget.winfo_reqheight()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
53 x = m_x + (m_width - w_width) * relx
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
54 y = m_y + (m_height - w_height) * rely
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
55 if x+w_width > master.winfo_screenwidth():
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
56 x = master.winfo_screenwidth() - w_width
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
57 elif x < 0:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
58 x = 0
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
59 if y+w_height > master.winfo_screenheight():
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
60 y = master.winfo_screenheight() - w_height
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
61 elif y < 0:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
62 y = 0
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
63 widget.geometry("+%d+%d" % (x, y))
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
64 widget.deiconify() # Become visible at the desired location
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
65
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
66 def go(self):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
67 self.root.wait_visibility()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
68 self.root.grab_set()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
69 self.root.mainloop()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
70 self.root.destroy()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
71 return self.num
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
72
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
73 def return_event(self, event):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
74 if self.default is None:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
75 self.root.bell()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
76 else:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
77 self.done(self.default)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
78
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
79 def wm_delete_window(self):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
80 if self.cancel is None:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
81 self.root.bell()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
82 else:
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
83 self.done(self.cancel)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
84
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
85 def done(self, num):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
86 self.num = num
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
87 self.root.quit()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
88
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
89
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
90 if __name__ == '__main__':
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
91
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
92 def test():
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
93 root = Tk()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
94 def doit(root=root):
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
95 d = SimpleDialog(root,
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
96 text="This is a test dialog. "
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
97 "Would this have been an actual dialog, "
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
98 "the buttons below would have been glowing "
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
99 "in soft pink light.\n"
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
100 "Do you believe this?",
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
101 buttons=["Yes", "No", "Cancel"],
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
102 default=0,
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
103 cancel=2,
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
104 title="Test Dialog")
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
105 print d.go()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
106 t = Button(root, text='Test', command=doit)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
107 t.pack()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
108 q = Button(root, text='Quit', command=t.quit)
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
109 q.pack()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
110 t.mainloop()
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
111
5cec436d64a2 Tkinter renaming reversal: move modules back in their place.
Georg Brandl <georg@python.org>
parents:
diff changeset
112 test()