1
"Dialog to specify or edit the parameters for a user configured help source."
7
import tkinter.messagebox as tkMessageBox
8
import tkinter.filedialog as tkFileDialog
10
class GetHelpSourceDialog(Toplevel):
11
def __init__(self, parent, title, menuItem='', filePath=''):
12
"""Get menu entry and url/ local file location for Additional Help
14
User selects a name for the Help resource and provides a web url
15
or a local file as its source. The user can enter a url or browse
19
Toplevel.__init__(self, parent)
20
self.configure(borderwidth=5)
21
self.resizable(height=FALSE, width=FALSE)
23
self.transient(parent)
25
self.protocol("WM_DELETE_WINDOW", self.Cancel)
29
self.menu.set(menuItem)
30
self.path.set(filePath)
31
self.withdraw() #hide while setting geometry
32
#needs to be done here so that the winfo_reqwidth is valid
33
self.update_idletasks()
34
#centre dialog over parent:
35
self.geometry("+%d+%d" %
36
((parent.winfo_rootx() + ((parent.winfo_width()/2)
37
-(self.winfo_reqwidth()/2)),
38
parent.winfo_rooty() + ((parent.winfo_height()/2)
39
-(self.winfo_reqheight()/2)))))
40
self.deiconify() #geometry set, unhide
41
self.bind('<Return>', self.Ok)
44
def CreateWidgets(self):
45
self.menu = StringVar(self)
46
self.path = StringVar(self)
47
self.fontSize = StringVar(self)
48
self.frameMain = Frame(self, borderwidth=2, relief=GROOVE)
49
self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH)
50
labelMenu = Label(self.frameMain, anchor=W, justify=LEFT,
52
self.entryMenu = Entry(self.frameMain, textvariable=self.menu,
54
self.entryMenu.focus_set()
55
labelPath = Label(self.frameMain, anchor=W, justify=LEFT,
56
text='Help File Path: Enter URL or browse for file')
57
self.entryPath = Entry(self.frameMain, textvariable=self.path,
59
self.entryMenu.focus_set()
60
labelMenu.pack(anchor=W, padx=5, pady=3)
61
self.entryMenu.pack(anchor=W, padx=5, pady=3)
62
labelPath.pack(anchor=W, padx=5, pady=3)
63
self.entryPath.pack(anchor=W, padx=5, pady=3)
64
browseButton = Button(self.frameMain, text='Browse', width=8,
65
command=self.browseFile)
66
browseButton.pack(pady=3)
67
frameButtons = Frame(self)
68
frameButtons.pack(side=BOTTOM, fill=X)
69
self.buttonOk = Button(frameButtons, text='OK',
70
width=8, default=ACTIVE, command=self.Ok)
71
self.buttonOk.grid(row=0, column=0, padx=5,pady=5)
72
self.buttonCancel = Button(frameButtons, text='Cancel',
73
width=8, command=self.Cancel)
74
self.buttonCancel.grid(row=0, column=1, padx=5, pady=5)
78
("HTML Files", "*.htm *.html", "TEXT"),
79
("PDF Files", "*.pdf", "TEXT"),
80
("Windows Help Files", "*.chm"),
81
("Text Files", "*.txt", "TEXT"),
83
path = self.path.get()
85
dir, base = os.path.split(path)
88
if sys.platform[:3] == 'win':
89
dir = os.path.join(os.path.dirname(sys.executable), 'Doc')
90
if not os.path.isdir(dir):
94
opendialog = tkFileDialog.Open(parent=self, filetypes=filetypes)
95
file = opendialog.show(initialdir=dir, initialfile=base)
100
"Simple validity check for a sensible menu item name"
102
menu = self.menu.get()
105
tkMessageBox.showerror(title='Menu Item Error',
106
message='No menu item specified',
108
self.entryMenu.focus_set()
111
tkMessageBox.showerror(title='Menu Item Error',
112
message='Menu item too long:'
113
'\nLimit 30 characters.',
115
self.entryMenu.focus_set()
120
"Simple validity check for menu file path"
122
path = self.path.get()
124
if not path: #no path specified
125
tkMessageBox.showerror(title='File Path Error',
126
message='No help file path specified.',
128
self.entryPath.focus_set()
130
elif path.startswith(('www.', 'http')):
133
if path[:5] == 'file:':
135
if not os.path.exists(path):
136
tkMessageBox.showerror(title='File Path Error',
137
message='Help file path does not exist.',
139
self.entryPath.focus_set()
143
def Ok(self, event=None):
144
if self.MenuOk() and self.PathOk():
145
self.result = (self.menu.get().strip(),
146
self.path.get().strip())
147
if sys.platform == 'darwin':
148
path = self.result[1]
149
if path.startswith(('www', 'file:', 'http:')):
152
# Mac Safari insists on using the URI form for local files
153
self.result = list(self.result)
154
self.result[1] = "file://" + path
157
def Cancel(self, event=None):
161
if __name__ == '__main__':
166
dlg = GetHelpSourceDialog(root, 'Get Help Source')
168
Button(root,text='Dialog', command=run).pack()