1 "Dialog to specify or edit the parameters for a user configured help source."
9 class GetHelpSourceDialog(Toplevel
):
10 def __init__(self
, parent
, title
, menuItem
='', filePath
=''):
11 """Get menu entry and url/ local file location for Additional Help
13 User selects a name for the Help resource and provides a web url
14 or a local file as its source. The user can enter a url or browse
18 Toplevel
.__init
__(self
, parent
)
19 self
.configure(borderwidth
=5)
20 self
.resizable(height
=FALSE
, width
=FALSE
)
22 self
.transient(parent
)
24 self
.protocol("WM_DELETE_WINDOW", self
.Cancel
)
28 self
.menu
.set(menuItem
)
29 self
.path
.set(filePath
)
30 self
.withdraw() #hide while setting geometry
31 #needs to be done here so that the winfo_reqwidth is valid
32 self
.update_idletasks()
33 #centre dialog over parent:
34 self
.geometry("+%d+%d" %
35 ((parent
.winfo_rootx() + ((parent
.winfo_width()/2)
36 -(self
.winfo_reqwidth()/2)),
37 parent
.winfo_rooty() + ((parent
.winfo_height()/2)
38 -(self
.winfo_reqheight()/2)))))
39 self
.deiconify() #geometry set, unhide
40 self
.bind('<Return>', self
.Ok
)
43 def CreateWidgets(self
):
44 self
.menu
= StringVar(self
)
45 self
.path
= StringVar(self
)
46 self
.fontSize
= StringVar(self
)
47 self
.frameMain
= Frame(self
, borderwidth
=2, relief
=GROOVE
)
48 self
.frameMain
.pack(side
=TOP
, expand
=TRUE
, fill
=BOTH
)
49 labelMenu
= Label(self
.frameMain
, anchor
=W
, justify
=LEFT
,
51 self
.entryMenu
= Entry(self
.frameMain
, textvariable
=self
.menu
,
53 self
.entryMenu
.focus_set()
54 labelPath
= Label(self
.frameMain
, anchor
=W
, justify
=LEFT
,
55 text
='Help File Path: Enter URL or browse for file')
56 self
.entryPath
= Entry(self
.frameMain
, textvariable
=self
.path
,
58 self
.entryMenu
.focus_set()
59 labelMenu
.pack(anchor
=W
, padx
=5, pady
=3)
60 self
.entryMenu
.pack(anchor
=W
, padx
=5, pady
=3)
61 labelPath
.pack(anchor
=W
, padx
=5, pady
=3)
62 self
.entryPath
.pack(anchor
=W
, padx
=5, pady
=3)
63 browseButton
= Button(self
.frameMain
, text
='Browse', width
=8,
64 command
=self
.browseFile
)
65 browseButton
.pack(pady
=3)
66 frameButtons
= Frame(self
)
67 frameButtons
.pack(side
=BOTTOM
, fill
=X
)
68 self
.buttonOk
= Button(frameButtons
, text
='OK',
69 width
=8, default
=ACTIVE
, command
=self
.Ok
)
70 self
.buttonOk
.grid(row
=0, column
=0, padx
=5,pady
=5)
71 self
.buttonCancel
= Button(frameButtons
, text
='Cancel',
72 width
=8, command
=self
.Cancel
)
73 self
.buttonCancel
.grid(row
=0, column
=1, padx
=5, pady
=5)
77 ("HTML Files", "*.htm *.html", "TEXT"),
78 ("PDF Files", "*.pdf", "TEXT"),
79 ("Windows Help Files", "*.chm"),
80 ("Text Files", "*.txt", "TEXT"),
82 path
= self
.path
.get()
84 dir, base
= os
.path
.split(path
)
87 if sys
.platform
.count('win') or sys
.platform
.count('nt'):
88 dir = os
.path
.join(os
.path
.dirname(sys
.executable
), 'Doc')
89 if not os
.path
.isdir(dir):
93 opendialog
= tkFileDialog
.Open(parent
=self
, filetypes
=filetypes
)
94 file = opendialog
.show(initialdir
=dir, initialfile
=base
)
99 "Simple validity check for a sensible menu item name"
101 menu
= self
.menu
.get()
104 tkMessageBox
.showerror(title
='Menu Item Error',
105 message
='No menu item specified',
107 self
.entryMenu
.focus_set()
110 tkMessageBox
.showerror(title
='Menu Item Error',
111 message
='Menu item too long:'
112 '\nLimit 30 characters.',
114 self
.entryMenu
.focus_set()
119 "Simple validity check for menu file path"
121 path
= self
.path
.get()
123 if not path
: #no path specified
124 tkMessageBox
.showerror(title
='File Path Error',
125 message
='No help file path specified.',
127 self
.entryPath
.focus_set()
129 elif path
.startswith('www.') or path
.startswith('http'):
131 elif not os
.path
.exists(path
):
132 tkMessageBox
.showerror(title
='File Path Error',
133 message
='Help file path does not exist.',
135 self
.entryPath
.focus_set()
139 def Ok(self
, event
=None):
140 if self
.MenuOk() and self
.PathOk():
141 self
.result
= (self
.menu
.get().strip(),
142 self
.path
.get().strip())
145 def Cancel(self
, event
=None):
149 if __name__
== '__main__':
154 dlg
= GetHelpSourceDialog(root
, 'Get Help Source')
156 Button(root
,text
='Dialog', command
=run
).pack()