2 Dialog that allows user to specify or edit the parameters for a user configured
9 class GetHelpSourceDialog(Toplevel
):
10 def __init__(self
,parent
,title
,menuItem
='',filePath
=''):
12 menuItem - string, the menu item to edit, if any
13 filePath - string, the help file path to edit, if any
15 Toplevel
.__init
__(self
, parent
)
16 self
.configure(borderwidth
=5)
17 self
.resizable(height
=FALSE
,width
=FALSE
)
19 self
.transient(parent
)
21 self
.protocol("WM_DELETE_WINDOW", self
.Cancel
)
25 self
.menu
.set(menuItem
)
26 self
.path
.set(filePath
)
27 self
.withdraw() #hide while setting geometry
28 self
.update_idletasks()
29 #needs to be done here so that the winfo_reqwidth is valid
30 self
.geometry("+%d+%d" %
31 ((parent
.winfo_rootx()+((parent
.winfo_width()/2)
32 -(self
.winfo_reqwidth()/2)),
33 parent
.winfo_rooty()+((parent
.winfo_height()/2)
34 -(self
.winfo_reqheight()/2)) )) ) #centre dialog over parent
35 self
.deiconify() #geometry set, unhide
38 def CreateWidgets(self
):
39 self
.menu
=StringVar(self
)
40 self
.path
=StringVar(self
)
41 self
.fontSize
=StringVar(self
)
42 self
.frameMain
= Frame(self
,borderwidth
=2,relief
=SUNKEN
)
43 self
.frameMain
.pack(side
=TOP
,expand
=TRUE
,fill
=BOTH
)
44 labelMenu
=Label(self
.frameMain
,anchor
=W
,justify
=LEFT
,
46 self
.entryMenu
=Entry(self
.frameMain
,textvariable
=self
.menu
,width
=30)
47 self
.entryMenu
.focus_set()
48 labelPath
=Label(self
.frameMain
,anchor
=W
,justify
=LEFT
,
49 text
='Help File Path:')
50 self
.entryPath
=Entry(self
.frameMain
,textvariable
=self
.path
,width
=40)
51 self
.entryMenu
.focus_set()
52 labelMenu
.pack(anchor
=W
,padx
=5,pady
=3)
53 self
.entryMenu
.pack(anchor
=W
,padx
=5,pady
=3)
54 labelPath
.pack(anchor
=W
,padx
=5,pady
=3)
55 self
.entryPath
.pack(anchor
=W
,padx
=5,pady
=3)
56 frameButtons
=Frame(self
)
57 frameButtons
.pack(side
=BOTTOM
,fill
=X
)
58 self
.buttonOk
= Button(frameButtons
,text
='Ok',
59 width
=8,command
=self
.Ok
)
60 self
.buttonOk
.grid(row
=0,column
=0,padx
=5,pady
=5)
61 self
.buttonCancel
= Button(frameButtons
,text
='Cancel',
62 width
=8,command
=self
.Cancel
)
63 self
.buttonCancel
.grid(row
=0,column
=1,padx
=5,pady
=5)
66 #simple validity check for a sensible
71 if not menu
: #no menu item specified
72 tkMessageBox
.showerror(title
='Menu Item Error',
73 message
='No menu item specified.')
74 self
.entryMenu
.focus_set()
76 elif len(menu
)>30: #menu item name too long
77 tkMessageBox
.showerror(title
='Menu Item Error',
78 message
='Menu item too long. It should be no more '+
79 'than 30 characters.')
80 self
.entryMenu
.focus_set()
85 #simple validity check for menu file path
89 if not path
: #no path specified
90 tkMessageBox
.showerror(title
='File Path Error',
91 message
='No help file path specified.')
92 self
.entryPath
.focus_set()
94 elif not os
.path
.exists(path
):
95 tkMessageBox
.showerror(title
='File Path Error',
96 message
='Help file path does not exist.')
97 self
.entryPath
.focus_set()
101 def Ok(self
, event
=None):
104 self
.result
=( self
.menu
.get().strip(),self
.path
.get().strip() )
107 def Cancel(self
, event
=None):
111 if __name__
== '__main__':
116 dlg
=GetHelpSourceDialog(root
,'Get Help Source')
118 Button(root
,text
='Dialog',command
=run
).pack()