1
"""A simple but flexible modal dialog box."""
9
def __init__(self, master,
10
text='', buttons=[], default=None, cancel=None,
11
title=None, class_=None):
13
self.root = Toplevel(master, class_=class_)
15
self.root = Toplevel(master)
17
self.root.title(title)
18
self.root.iconname(title)
19
self.message = Message(self.root, text=text, aspect=400)
20
self.message.pack(expand=1, fill=BOTH)
21
self.frame = Frame(self.root)
25
self.default = default
26
self.root.bind('<Return>', self.return_event)
27
for num in range(len(buttons)):
29
b = Button(self.frame, text=s,
30
command=(lambda self=self, num=num: self.done(num)))
32
b.config(relief=RIDGE, borderwidth=8)
33
b.pack(side=LEFT, fill=BOTH, expand=1)
34
self.root.protocol('WM_DELETE_WINDOW', self.wm_delete_window)
35
self._set_transient(master)
37
def _set_transient(self, master, relx=0.5, rely=0.3):
39
widget.withdraw() # Remain invisible while we figure out the geometry
40
widget.transient(master)
41
widget.update_idletasks() # Actualize geometry information
42
if master.winfo_ismapped():
43
m_width = master.winfo_width()
44
m_height = master.winfo_height()
45
m_x = master.winfo_rootx()
46
m_y = master.winfo_rooty()
48
m_width = master.winfo_screenwidth()
49
m_height = master.winfo_screenheight()
51
w_width = widget.winfo_reqwidth()
52
w_height = widget.winfo_reqheight()
53
x = m_x + (m_width - w_width) * relx
54
y = m_y + (m_height - w_height) * rely
55
if x+w_width > master.winfo_screenwidth():
56
x = master.winfo_screenwidth() - w_width
59
if y+w_height > master.winfo_screenheight():
60
y = master.winfo_screenheight() - w_height
63
widget.geometry("+%d+%d" % (x, y))
64
widget.deiconify() # Become visible at the desired location
67
self.root.wait_visibility()
73
def return_event(self, event):
74
if self.default is None:
77
self.done(self.default)
79
def wm_delete_window(self):
80
if self.cancel is None:
83
self.done(self.cancel)
90
if __name__ == '__main__':
95
d = SimpleDialog(root,
96
text="This is a test dialog. "
97
"Would this have been an actual dialog, "
98
"the buttons below would have been glowing "
99
"in soft pink light.\n"
100
"Do you believe this?",
101
buttons=["Yes", "No", "Cancel"],
106
t = Button(root, text='Test', command=doit)
108
q = Button(root, text='Quit', command=t.quit)