0001 """A simple but flexible modal dialog box.""" 0002 0003 0004 from Tkinter import * 0005 0006 0007 class SimpleDialog: 0008 0009 def __init__(self, master, 0010 text='', buttons=[], default=None, cancel=None, 0011 title=None, class_=None): 0012 if class_: 0013 self.root = Toplevel(master, class_=class_) 0014 else: 0015 self.root = Toplevel(master) 0016 if title: 0017 self.root.title(title) 0018 self.root.iconname(title) 0019 self.message = Message(self.root, text=text, aspect=400) 0020 self.message.pack(expand=1, fill=BOTH) 0021 self.frame = Frame(self.root) 0022 self.frame.pack() 0023 self.num = default 0024 self.cancel = cancel 0025 self.default = default 0026 self.root.bind('<Return>', self.return_event) 0027 for num in range(len(buttons)): 0028 s = buttons[num] 0029 b = Button(self.frame, text=s, 0030 command=(lambda self=self, num=num: self.done(num))) 0031 if num == default: 0032 b.config(relief=RIDGE, borderwidth=8) 0033 b.pack(side=LEFT, fill=BOTH, expand=1) 0034 self.root.protocol('WM_DELETE_WINDOW', self.wm_delete_window) 0035 self._set_transient(master) 0036 0037 def _set_transient(self, master, relx=0.5, rely=0.3): 0038 widget = self.root 0039 widget.withdraw() # Remain invisible while we figure out the geometry 0040 widget.transient(master) 0041 widget.update_idletasks() # Actualize geometry information 0042 if master.winfo_ismapped(): 0043 m_width = master.winfo_width() 0044 m_height = master.winfo_height() 0045 m_x = master.winfo_rootx() 0046 m_y = master.winfo_rooty() 0047 else: 0048 m_width = master.winfo_screenwidth() 0049 m_height = master.winfo_screenheight() 0050 m_x = m_y = 0 0051 w_width = widget.winfo_reqwidth() 0052 w_height = widget.winfo_reqheight() 0053 x = m_x + (m_width - w_width) * relx 0054 y = m_y + (m_height - w_height) * rely 0055 if x+w_width > master.winfo_screenwidth(): 0056 x = master.winfo_screenwidth() - w_width 0057 elif x < 0: 0058 x = 0 0059 if y+w_height > master.winfo_screenheight(): 0060 y = master.winfo_screenheight() - w_height 0061 elif y < 0: 0062 y = 0 0063 widget.geometry("+%d+%d" % (x, y)) 0064 widget.deiconify() # Become visible at the desired location 0065 0066 def go(self): 0067 self.root.wait_visibility() 0068 self.root.grab_set() 0069 self.root.mainloop() 0070 self.root.destroy() 0071 return self.num 0072 0073 def return_event(self, event): 0074 if self.default is None: 0075 self.root.bell() 0076 else: 0077 self.done(self.default) 0078 0079 def wm_delete_window(self): 0080 if self.cancel is None: 0081 self.root.bell() 0082 else: 0083 self.done(self.cancel) 0084 0085 def done(self, num): 0086 self.num = num 0087 self.root.quit() 0088 0089 0090 if __name__ == '__main__': 0091 0092 def test(): 0093 root = Tk() 0094 def doit(root=root): 0095 d = SimpleDialog(root, 0096 text="This is a test dialog. " 0097 "Would this have been an actual dialog, " 0098 "the buttons below would have been glowing " 0099 "in soft pink light.\n" 0100 "Do you believe this?", 0101 buttons=["Yes", "No", "Cancel"], 0102 default=0, 0103 cancel=2, 0104 title="Test Dialog") 0105 print d.go() 0106 t = Button(root, text='Test', command=doit) 0107 t.pack() 0108 q = Button(root, text='Quit', command=t.quit) 0109 q.pack() 0110 t.mainloop() 0111 0112 test() 0113
Generated by PyXR 0.9.4