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