Debian Codegraph @

modules/pmg_tk/skins/normal/ModalWindow.py

raw symbols SCIP
1
2 import tkinter as Tk
3
4 class ModalWindow(Tk.Toplevel):
5 #
6 # Makes a toplevel window that is model (won't let you access the parent window when this window is open)
7 # self.pack_and_display() = packs any buttons in the window and displays the window
8 #
9 # parent = window to display on top of
10 # buttons = True to include a default set of "Accept" and "Cancel" buttons in the window
11 # user_accept = the function that the "Accept" button should call before destroying itself
12 # user_cancel = the function that the "Cancel" button should call before destroying itself
13 #
14 def __init__(self, parent, title='modal window', buttons=False, user_accept=False, user_cancel=False, *posargs, **kwargs):
15 Tk.Toplevel.__init__(self, *posargs, **kwargs)
16 self.parent_window = parent
17 self.buttons = buttons
18 self.user_accept = user_accept
19 self.user_cancel = user_cancel
20 self.parent_window = parent
21 self.title(title)
22 self.wm_protocol('WM_DELETE_WINDOW', self.cancel)
23 self.withdraw()
24
25 def cancel(self):
26 # The user should define a function and assign it to the modal window as follows:
27 # bob = modal_window(parent, user_cancel=function)
28 if self.user_cancel:
29 self.user_cancel()
30 self.destroy()
31
32 def accept(self,event):
33 # The user should define a function and assign it to the modal window as follows:
34 # bob = modal_window(parent, user_accept=function)
35 if self.user_accept:
36 self.user_accept(event)
37 self.grab_release()
38 self.destroy()
39
40 def display(self, grab=True):
41 #print "ModalWindow::display"
42 # Don't include the height and width in the geometry string or else the window doesn't automatically resize
43 self.update_idletasks()
44 self.deiconify()
45 # Transient makes this window not show up in the taskbar, and iconify, etc. with parent
46 self.transient(self.parent_window)
47 # focus_set makes this window active, which Windows doesn't always do without it
48 self.focus_set()
49
50 # The grab_set/wait_window combo means that this window stays on top and the parent
51 # window doesn't respond to any mouseclicks
52 if grab:
53 # We may want to set grab=False if this window has widgets that generate another toplevel window
54 # (such as a DropDownCombobox)
55 self.grab_set()
56 #self.parent_window.wait_visibility(self.parent_window)
57 self.parent_window.wait_window(self)
58
59 def pack(self,x,y):
60 if self.buttons:
61 frame = Tk.Frame(self)
62 accept_button = Tk.Button(frame, text='Accept', command=self.accept)
63 cancel_button = Tk.Button(frame, text='Cancel', command=self.cancel)
64 frame.pack(fill=Tk.X)
65 accept_button.pack(side=Tk.LEFT)
66 cancel_button.pack(side=Tk.RIGHT)
67 self.buttons = False
68 #print "Pack=(%d,%d)" % (x,y)
69
70 def pack_and_display(self,event=None,grab=True):
71 #print "ModalWindow::pack_and_display"
72 wgt = event.widget
73 (x,y) = wgt.canvasx(event.x), wgt.canvasy(event.y)
74 self.pack(x,y)
75 self.display(grab=grab)
76
77 def get_val():
78 myvar.set(entry.get())
79
80
81 if __name__=="__main__":
82 root = Tk.Tk()
83 myvar = Tk.StringVar()
84 myvar.set('Hello')
85 label = Tk.Label(textvariable=myvar)
86 mywin = ModalWindow(root, buttons=True, user_accept=get_val)
87 button = Tk.Button(root, text='Show window', command=mywin.pack_and_display)
88 entry = Tk.Entry(mywin)
89 entry.pack()
90 label.pack()
91 button.pack()
92 root.mainloop()
93