Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 1 | """ |
| 2 | Dialog that allows user to specify or edit the parameters for a user configured |
| 3 | help source. |
| 4 | """ |
| 5 | from Tkinter import * |
| 6 | import tkMessageBox |
| 7 | import os |
| 8 | |
| 9 | class GetHelpSourceDialog(Toplevel): |
| 10 | def __init__(self,parent,title,menuItem='',filePath=''): |
| 11 | """ |
| 12 | menuItem - string, the menu item to edit, if any |
| 13 | filePath - string, the help file path to edit, if any |
| 14 | """ |
| 15 | Toplevel.__init__(self, parent) |
| 16 | self.configure(borderwidth=5) |
| 17 | self.resizable(height=FALSE,width=FALSE) |
| 18 | self.title(title) |
| 19 | self.transient(parent) |
| 20 | self.grab_set() |
| 21 | self.protocol("WM_DELETE_WINDOW", self.Cancel) |
| 22 | self.parent = parent |
| 23 | self.result=None |
| 24 | self.CreateWidgets() |
Steven M. Gava | 0c5bc8c | 2002-03-27 02:25:44 +0000 | [diff] [blame] | 25 | self.menu.set(menuItem) |
| 26 | self.path.set(filePath) |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 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 |
| 36 | self.wait_window() |
| 37 | |
| 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, |
| 45 | text='Menu Item:') |
| 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) |
| 64 | |
| 65 | def MenuOk(self): |
| 66 | #simple validity check for a sensible |
| 67 | #menu item name |
| 68 | menuOk=1 |
| 69 | menu=self.menu.get() |
| 70 | menu.strip() |
| 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() |
| 75 | menuOk=0 |
| 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() |
| 81 | menuOk=0 |
| 82 | return menuOk |
| 83 | |
| 84 | def PathOk(self): |
| 85 | #simple validity check for menu file path |
| 86 | pathOk=1 |
| 87 | path=self.path.get() |
| 88 | path.strip() |
| 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() |
| 93 | pathOk=0 |
| 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() |
| 98 | pathOk=0 |
| 99 | return pathOk |
| 100 | |
| 101 | def Ok(self, event=None): |
| 102 | if self.MenuOk(): |
| 103 | if self.PathOk(): |
| 104 | self.result=( self.menu.get().strip(),self.path.get().strip() ) |
| 105 | self.destroy() |
| 106 | |
| 107 | def Cancel(self, event=None): |
| 108 | self.result=None |
| 109 | self.destroy() |
| 110 | |
| 111 | if __name__ == '__main__': |
| 112 | #test the dialog |
| 113 | root=Tk() |
| 114 | def run(): |
| 115 | keySeq='' |
| 116 | dlg=GetHelpSourceDialog(root,'Get Help Source') |
| 117 | print dlg.result |
| 118 | Button(root,text='Dialog',command=run).pack() |
| 119 | root.mainloop() |
| 120 | |
| 121 | |