0001 "Dialog to specify or edit the parameters for a user configured help source." 0002 0003 import os 0004 import sys 0005 0006 from Tkinter import * 0007 import tkMessageBox 0008 import tkFileDialog 0009 0010 class GetHelpSourceDialog(Toplevel): 0011 def __init__(self, parent, title, menuItem='', filePath=''): 0012 """Get menu entry and url/ local file location for Additional Help 0013 0014 User selects a name for the Help resource and provides a web url 0015 or a local file as its source. The user can enter a url or browse 0016 for the file. 0017 0018 """ 0019 Toplevel.__init__(self, parent) 0020 self.configure(borderwidth=5) 0021 self.resizable(height=FALSE, width=FALSE) 0022 self.title(title) 0023 self.transient(parent) 0024 self.grab_set() 0025 self.protocol("WM_DELETE_WINDOW", self.Cancel) 0026 self.parent = parent 0027 self.result = None 0028 self.CreateWidgets() 0029 self.menu.set(menuItem) 0030 self.path.set(filePath) 0031 self.withdraw() #hide while setting geometry 0032 #needs to be done here so that the winfo_reqwidth is valid 0033 self.update_idletasks() 0034 #centre dialog over parent: 0035 self.geometry("+%d+%d" % 0036 ((parent.winfo_rootx() + ((parent.winfo_width()/2) 0037 -(self.winfo_reqwidth()/2)), 0038 parent.winfo_rooty() + ((parent.winfo_height()/2) 0039 -(self.winfo_reqheight()/2))))) 0040 self.deiconify() #geometry set, unhide 0041 self.bind('<Return>', self.Ok) 0042 self.wait_window() 0043 0044 def CreateWidgets(self): 0045 self.menu = StringVar(self) 0046 self.path = StringVar(self) 0047 self.fontSize = StringVar(self) 0048 self.frameMain = Frame(self, borderwidth=2, relief=GROOVE) 0049 self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH) 0050 labelMenu = Label(self.frameMain, anchor=W, justify=LEFT, 0051 text='Menu Item:') 0052 self.entryMenu = Entry(self.frameMain, textvariable=self.menu, 0053 width=30) 0054 self.entryMenu.focus_set() 0055 labelPath = Label(self.frameMain, anchor=W, justify=LEFT, 0056 text='Help File Path: Enter URL or browse for file') 0057 self.entryPath = Entry(self.frameMain, textvariable=self.path, 0058 width=40) 0059 self.entryMenu.focus_set() 0060 labelMenu.pack(anchor=W, padx=5, pady=3) 0061 self.entryMenu.pack(anchor=W, padx=5, pady=3) 0062 labelPath.pack(anchor=W, padx=5, pady=3) 0063 self.entryPath.pack(anchor=W, padx=5, pady=3) 0064 browseButton = Button(self.frameMain, text='Browse', width=8, 0065 command=self.browseFile) 0066 browseButton.pack(pady=3) 0067 frameButtons = Frame(self) 0068 frameButtons.pack(side=BOTTOM, fill=X) 0069 self.buttonOk = Button(frameButtons, text='OK', 0070 width=8, default=ACTIVE, command=self.Ok) 0071 self.buttonOk.grid(row=0, column=0, padx=5,pady=5) 0072 self.buttonCancel = Button(frameButtons, text='Cancel', 0073 width=8, command=self.Cancel) 0074 self.buttonCancel.grid(row=0, column=1, padx=5, pady=5) 0075 0076 def browseFile(self): 0077 filetypes = [ 0078 ("HTML Files", "*.htm *.html", "TEXT"), 0079 ("PDF Files", "*.pdf", "TEXT"), 0080 ("Windows Help Files", "*.chm"), 0081 ("Text Files", "*.txt", "TEXT"), 0082 ("All Files", "*")] 0083 path = self.path.get() 0084 if path: 0085 dir, base = os.path.split(path) 0086 else: 0087 base = None 0088 if sys.platform[:3] == 'win': 0089 dir = os.path.join(os.path.dirname(sys.executable), 'Doc') 0090 if not os.path.isdir(dir): 0091 dir = os.getcwd() 0092 else: 0093 dir = os.getcwd() 0094 opendialog = tkFileDialog.Open(parent=self, filetypes=filetypes) 0095 file = opendialog.show(initialdir=dir, initialfile=base) 0096 if file: 0097 self.path.set(file) 0098 0099 def MenuOk(self): 0100 "Simple validity check for a sensible menu item name" 0101 menuOk = True 0102 menu = self.menu.get() 0103 menu.strip() 0104 if not menu: 0105 tkMessageBox.showerror(title='Menu Item Error', 0106 message='No menu item specified', 0107 parent=self) 0108 self.entryMenu.focus_set() 0109 menuOk = False 0110 elif len(menu) > 30: 0111 tkMessageBox.showerror(title='Menu Item Error', 0112 message='Menu item too long:' 0113 '\nLimit 30 characters.', 0114 parent=self) 0115 self.entryMenu.focus_set() 0116 menuOk = False 0117 return menuOk 0118 0119 def PathOk(self): 0120 "Simple validity check for menu file path" 0121 pathOk = True 0122 path = self.path.get() 0123 path.strip() 0124 if not path: #no path specified 0125 tkMessageBox.showerror(title='File Path Error', 0126 message='No help file path specified.', 0127 parent=self) 0128 self.entryPath.focus_set() 0129 pathOk = False 0130 elif path.startswith('www.') or path.startswith('http'): 0131 pass 0132 else: 0133 if path[:5] == 'file:': 0134 path = path[5:] 0135 if not os.path.exists(path): 0136 tkMessageBox.showerror(title='File Path Error', 0137 message='Help file path does not exist.', 0138 parent=self) 0139 self.entryPath.focus_set() 0140 pathOk = False 0141 return pathOk 0142 0143 def Ok(self, event=None): 0144 if self.MenuOk() and self.PathOk(): 0145 self.result = (self.menu.get().strip(), 0146 self.path.get().strip()) 0147 if sys.platform == 'darwin': 0148 path = self.result[1] 0149 if (path.startswith('www') or path.startswith('file:') 0150 or path.startswith('http:')): 0151 pass 0152 else: 0153 # Mac Safari insists on using the URI form for local files 0154 self.result[1] = "file://" + path 0155 self.destroy() 0156 0157 def Cancel(self, event=None): 0158 self.result = None 0159 self.destroy() 0160 0161 if __name__ == '__main__': 0162 #test the dialog 0163 root = Tk() 0164 def run(): 0165 keySeq = '' 0166 dlg = GetHelpSourceDialog(root, 'Get Help Source') 0167 print dlg.result 0168 Button(root,text='Dialog', command=run).pack() 0169 root.mainloop() 0170
Generated by PyXR 0.9.4