blob: 7354779c8058f9f98dbfb214d6eb97b122ff8082 [file] [log] [blame]
Guido van Rossum990e6191996-06-17 17:14:46 +00001"""A simple but flexible modal dialog box."""
2
3
4from Tkinter import *
Guido van Rossum990e6191996-06-17 17:14:46 +00005
6
7class SimpleDialog:
8
9 def __init__(self, master,
10 text='', buttons=[], default=None, cancel=None,
11 title=None, class_=None):
Guido van Rossum17f2b2d1996-07-21 02:20:06 +000012 if class_:
13 self.root = Toplevel(master, class_=class_)
14 else:
15 self.root = Toplevel(master)
16 if title:
17 self.root.title(title)
18 self.root.iconname(title)
Guido van Rossum990e6191996-06-17 17:14:46 +000019 self.message = Message(self.root, text=text, aspect=400)
20 self.message.pack(expand=1, fill=BOTH)
21 self.frame = Frame(self.root)
22 self.frame.pack()
23 self.num = default
24 self.cancel = cancel
25 self.default = default
26 self.root.bind('<Return>', self.return_event)
27 for num in range(len(buttons)):
28 s = buttons[num]
29 b = Button(self.frame, text=s,
30 command=(lambda self=self, num=num: self.done(num)))
31 if num == default:
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)
Guido van Rossum17f2b2d1996-07-21 02:20:06 +000035 self._set_transient(master)
36
37 def _set_transient(self, master, relx=0.5, rely=0.3):
38 widget = self.root
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()
47 else:
48 m_width = master.winfo_screenwidth()
49 m_height = master.winfo_screenheight()
50 m_x = m_y = 0
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 widget.geometry("+%d+%d" % (x, y))
56 widget.deiconify() # Become visible at the desired location
Guido van Rossum990e6191996-06-17 17:14:46 +000057
58 def go(self):
59 self.root.grab_set()
60 self.root.mainloop()
61 self.root.destroy()
62 return self.num
63
64 def return_event(self, event):
65 if self.default is None:
66 self.root.bell()
67 else:
68 self.done(self.default)
69
70 def wm_delete_window(self):
71 if self.cancel is None:
72 self.root.bell()
73 else:
74 self.done(self.cancel)
75
76 def done(self, num):
77 self.num = num
78 self.root.quit()
79
80
81def test():
82 root = Tk()
83 def doit(root=root):
84 d = SimpleDialog(root,
85 text="This is a test dialog. "
86 "Would this have been an actual dialog, "
Guido van Rossum17f2b2d1996-07-21 02:20:06 +000087 "the buttons below would have been glowing "
88 "in soft pink light.\n"
Guido van Rossum990e6191996-06-17 17:14:46 +000089 "Do you believe this?",
90 buttons=["Yes", "No", "Cancel"],
91 default=0,
92 cancel=2,
93 title="Test Dialog")
94 print d.go()
95 t = Button(root, text='Test', command=doit)
96 t.pack()
97 q = Button(root, text='Quit', command=t.quit)
98 q.pack()
99 t.mainloop()
100
101
102if __name__ == '__main__':
103 test()