annotate Lib/lib-tk/SimpleDialog.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 9c37ed8868b9
children e02792112288 29be729b32c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """A simple but flexible modal dialog box."""
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 from Tkinter import *
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 class SimpleDialog:
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 def __init__(self, master,
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
10 text='', buttons=[], default=None, cancel=None,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
11 title=None, class_=None):
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
12 if class_:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
13 self.root = Toplevel(master, class_=class_)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
14 else:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
15 self.root = Toplevel(master)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
16 if title:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
17 self.root.title(title)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
18 self.root.iconname(title)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
19 self.message = Message(self.root, text=text, aspect=400)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
20 self.message.pack(expand=1, fill=BOTH)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
21 self.frame = Frame(self.root)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
22 self.frame.pack()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
23 self.num = default
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
24 self.cancel = cancel
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
25 self.default = default
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
26 self.root.bind('<Return>', self.return_event)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
27 for num in range(len(buttons)):
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
28 s = buttons[num]
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
29 b = Button(self.frame, text=s,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
30 command=(lambda self=self, num=num: self.done(num)))
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
31 if num == default:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
32 b.config(relief=RIDGE, borderwidth=8)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
33 b.pack(side=LEFT, fill=BOTH, expand=1)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
34 self.root.protocol('WM_DELETE_WINDOW', self.wm_delete_window)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
35 self._set_transient(master)
3757
15b1b03ade93 Don't use tktools, dummy! Reworded test message slightly.
Guido van Rossum <guido@python.org>
parents: 3688
diff changeset
36
15b1b03ade93 Don't use tktools, dummy! Reworded test message slightly.
Guido van Rossum <guido@python.org>
parents: 3688
diff changeset
37 def _set_transient(self, master, relx=0.5, rely=0.3):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
38 widget = self.root
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
39 widget.withdraw() # Remain invisible while we figure out the geometry
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
40 widget.transient(master)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
41 widget.update_idletasks() # Actualize geometry information
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
42 if master.winfo_ismapped():
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
43 m_width = master.winfo_width()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
44 m_height = master.winfo_height()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
45 m_x = master.winfo_rootx()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
46 m_y = master.winfo_rooty()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
47 else:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
48 m_width = master.winfo_screenwidth()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
49 m_height = master.winfo_screenheight()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
50 m_x = m_y = 0
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
51 w_width = widget.winfo_reqwidth()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
52 w_height = widget.winfo_reqheight()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
53 x = m_x + (m_width - w_width) * relx
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
54 y = m_y + (m_height - w_height) * rely
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
55 if x+w_width > master.winfo_screenwidth():
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
56 x = master.winfo_screenwidth() - w_width
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
57 elif x < 0:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
58 x = 0
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
59 if y+w_height > master.winfo_screenheight():
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
60 y = master.winfo_screenheight() - w_height
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
61 elif y < 0:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
62 y = 0
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
63 widget.geometry("+%d+%d" % (x, y))
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
64 widget.deiconify() # Become visible at the desired location
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 def go(self):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
67 self.root.grab_set()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
68 self.root.mainloop()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
69 self.root.destroy()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
70 return self.num
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 def return_event(self, event):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
73 if self.default is None:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
74 self.root.bell()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
75 else:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
76 self.done(self.default)
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 def wm_delete_window(self):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
79 if self.cancel is None:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
80 self.root.bell()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
81 else:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
82 self.done(self.cancel)
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 def done(self, num):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
85 self.num = num
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
86 self.root.quit()
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 def test():
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 root = Tk()
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 def doit(root=root):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
92 d = SimpleDialog(root,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
93 text="This is a test dialog. "
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
94 "Would this have been an actual dialog, "
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
95 "the buttons below would have been glowing "
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
96 "in soft pink light.\n"
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
97 "Do you believe this?",
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
98 buttons=["Yes", "No", "Cancel"],
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
99 default=0,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
100 cancel=2,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
101 title="Test Dialog")
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 6948
diff changeset
102 print d.go()
3688
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 t = Button(root, text='Test', command=doit)
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 t.pack()
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 q = Button(root, text='Quit', command=t.quit)
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 q.pack()
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 t.mainloop()
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 if __name__ == '__main__':
a79b8d81997f new simple dialog module, incompatible
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 test()