1 | "Dialog to specify or edit the parameters for a user configured help source."
|
---|
2 |
|
---|
3 | import os
|
---|
4 | import sys
|
---|
5 |
|
---|
6 | from Tkinter import *
|
---|
7 | import tkMessageBox
|
---|
8 | import tkFileDialog
|
---|
9 |
|
---|
10 | class GetHelpSourceDialog(Toplevel):
|
---|
11 | def __init__(self, parent, title, menuItem='', filePath=''):
|
---|
12 | """Get menu entry and url/ local file location for Additional Help
|
---|
13 |
|
---|
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
|
---|
16 | for the file.
|
---|
17 |
|
---|
18 | """
|
---|
19 | Toplevel.__init__(self, parent)
|
---|
20 | self.configure(borderwidth=5)
|
---|
21 | self.resizable(height=FALSE, width=FALSE)
|
---|
22 | self.title(title)
|
---|
23 | self.transient(parent)
|
---|
24 | self.grab_set()
|
---|
25 | self.protocol("WM_DELETE_WINDOW", self.Cancel)
|
---|
26 | self.parent = parent
|
---|
27 | self.result = None
|
---|
28 | self.CreateWidgets()
|
---|
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)
|
---|
42 | self.wait_window()
|
---|
43 |
|
---|
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,
|
---|
51 | text='Menu Item:')
|
---|
52 | self.entryMenu = Entry(self.frameMain, textvariable=self.menu,
|
---|
53 | width=30)
|
---|
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,
|
---|
58 | width=40)
|
---|
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)
|
---|
75 |
|
---|
76 | def browseFile(self):
|
---|
77 | filetypes = [
|
---|
78 | ("HTML Files", "*.htm *.html", "TEXT"),
|
---|
79 | ("PDF Files", "*.pdf", "TEXT"),
|
---|
80 | ("Windows Help Files", "*.chm"),
|
---|
81 | ("Text Files", "*.txt", "TEXT"),
|
---|
82 | ("All Files", "*")]
|
---|
83 | path = self.path.get()
|
---|
84 | if path:
|
---|
85 | dir, base = os.path.split(path)
|
---|
86 | else:
|
---|
87 | base = None
|
---|
88 | if sys.platform[:3] == 'win':
|
---|
89 | dir = os.path.join(os.path.dirname(sys.executable), 'Doc')
|
---|
90 | if not os.path.isdir(dir):
|
---|
91 | dir = os.getcwd()
|
---|
92 | else:
|
---|
93 | dir = os.getcwd()
|
---|
94 | opendialog = tkFileDialog.Open(parent=self, filetypes=filetypes)
|
---|
95 | file = opendialog.show(initialdir=dir, initialfile=base)
|
---|
96 | if file:
|
---|
97 | self.path.set(file)
|
---|
98 |
|
---|
99 | def MenuOk(self):
|
---|
100 | "Simple validity check for a sensible menu item name"
|
---|
101 | menuOk = True
|
---|
102 | menu = self.menu.get()
|
---|
103 | menu.strip()
|
---|
104 | if not menu:
|
---|
105 | tkMessageBox.showerror(title='Menu Item Error',
|
---|
106 | message='No menu item specified',
|
---|
107 | parent=self)
|
---|
108 | self.entryMenu.focus_set()
|
---|
109 | menuOk = False
|
---|
110 | elif len(menu) > 30:
|
---|
111 | tkMessageBox.showerror(title='Menu Item Error',
|
---|
112 | message='Menu item too long:'
|
---|
113 | '\nLimit 30 characters.',
|
---|
114 | parent=self)
|
---|
115 | self.entryMenu.focus_set()
|
---|
116 | menuOk = False
|
---|
117 | return menuOk
|
---|
118 |
|
---|
119 | def PathOk(self):
|
---|
120 | "Simple validity check for menu file path"
|
---|
121 | pathOk = True
|
---|
122 | path = self.path.get()
|
---|
123 | path.strip()
|
---|
124 | if not path: #no path specified
|
---|
125 | tkMessageBox.showerror(title='File Path Error',
|
---|
126 | message='No help file path specified.',
|
---|
127 | parent=self)
|
---|
128 | self.entryPath.focus_set()
|
---|
129 | pathOk = False
|
---|
130 | elif path.startswith(('www.', 'http')):
|
---|
131 | pass
|
---|
132 | else:
|
---|
133 | if path[:5] == 'file:':
|
---|
134 | path = path[5:]
|
---|
135 | if not os.path.exists(path):
|
---|
136 | tkMessageBox.showerror(title='File Path Error',
|
---|
137 | message='Help file path does not exist.',
|
---|
138 | parent=self)
|
---|
139 | self.entryPath.focus_set()
|
---|
140 | pathOk = False
|
---|
141 | return pathOk
|
---|
142 |
|
---|
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:')):
|
---|
150 | pass
|
---|
151 | else:
|
---|
152 | # Mac Safari insists on using the URI form for local files
|
---|
153 | self.result = list(self.result)
|
---|
154 | self.result[1] = "file://" + path
|
---|
155 | self.destroy()
|
---|
156 |
|
---|
157 | def Cancel(self, event=None):
|
---|
158 | self.result = None
|
---|
159 | self.destroy()
|
---|
160 |
|
---|
161 | if __name__ == '__main__':
|
---|
162 | #test the dialog
|
---|
163 | root = Tk()
|
---|
164 | def run():
|
---|
165 | keySeq = ''
|
---|
166 | dlg = GetHelpSourceDialog(root, 'Get Help Source')
|
---|
167 | print dlg.result
|
---|
168 | Button(root,text='Dialog', command=run).pack()
|
---|
169 | root.mainloop()
|
---|