blob: f5932146bd0f6023fe8b3715df700047339b6e30 [file] [log] [blame]
Steven M. Gava085eb1b2002-02-05 04:52:32 +00001"""
2Dialog that allows user to specify or edit the parameters for a user configured
3help source.
4"""
5from Tkinter import *
6import tkMessageBox
7import os
8
9class 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()
25 self.withdraw() #hide while setting geometry
26 self.update_idletasks()
27 #needs to be done here so that the winfo_reqwidth is valid
28 self.geometry("+%d+%d" %
29 ((parent.winfo_rootx()+((parent.winfo_width()/2)
30 -(self.winfo_reqwidth()/2)),
31 parent.winfo_rooty()+((parent.winfo_height()/2)
32 -(self.winfo_reqheight()/2)) )) ) #centre dialog over parent
33 self.deiconify() #geometry set, unhide
34 self.wait_window()
35
36 def CreateWidgets(self):
37 self.menu=StringVar(self)
38 self.path=StringVar(self)
39 self.fontSize=StringVar(self)
40 self.frameMain = Frame(self,borderwidth=2,relief=SUNKEN)
41 self.frameMain.pack(side=TOP,expand=TRUE,fill=BOTH)
42 labelMenu=Label(self.frameMain,anchor=W,justify=LEFT,
43 text='Menu Item:')
44 self.entryMenu=Entry(self.frameMain,textvariable=self.menu,width=30)
45 self.entryMenu.focus_set()
46 labelPath=Label(self.frameMain,anchor=W,justify=LEFT,
47 text='Help File Path:')
48 self.entryPath=Entry(self.frameMain,textvariable=self.path,width=40)
49 self.entryMenu.focus_set()
50 labelMenu.pack(anchor=W,padx=5,pady=3)
51 self.entryMenu.pack(anchor=W,padx=5,pady=3)
52 labelPath.pack(anchor=W,padx=5,pady=3)
53 self.entryPath.pack(anchor=W,padx=5,pady=3)
54 frameButtons=Frame(self)
55 frameButtons.pack(side=BOTTOM,fill=X)
56 self.buttonOk = Button(frameButtons,text='Ok',
57 width=8,command=self.Ok)
58 self.buttonOk.grid(row=0,column=0,padx=5,pady=5)
59 self.buttonCancel = Button(frameButtons,text='Cancel',
60 width=8,command=self.Cancel)
61 self.buttonCancel.grid(row=0,column=1,padx=5,pady=5)
62
63 def MenuOk(self):
64 #simple validity check for a sensible
65 #menu item name
66 menuOk=1
67 menu=self.menu.get()
68 menu.strip()
69 if not menu: #no menu item specified
70 tkMessageBox.showerror(title='Menu Item Error',
71 message='No menu item specified.')
72 self.entryMenu.focus_set()
73 menuOk=0
74 elif len(menu)>30: #menu item name too long
75 tkMessageBox.showerror(title='Menu Item Error',
76 message='Menu item too long. It should be no more '+
77 'than 30 characters.')
78 self.entryMenu.focus_set()
79 menuOk=0
80 return menuOk
81
82 def PathOk(self):
83 #simple validity check for menu file path
84 pathOk=1
85 path=self.path.get()
86 path.strip()
87 if not path: #no path specified
88 tkMessageBox.showerror(title='File Path Error',
89 message='No help file path specified.')
90 self.entryPath.focus_set()
91 pathOk=0
92 elif not os.path.exists(path):
93 tkMessageBox.showerror(title='File Path Error',
94 message='Help file path does not exist.')
95 self.entryPath.focus_set()
96 pathOk=0
97 return pathOk
98
99 def Ok(self, event=None):
100 if self.MenuOk():
101 if self.PathOk():
102 self.result=( self.menu.get().strip(),self.path.get().strip() )
103 self.destroy()
104
105 def Cancel(self, event=None):
106 self.result=None
107 self.destroy()
108
109if __name__ == '__main__':
110 #test the dialog
111 root=Tk()
112 def run():
113 keySeq=''
114 dlg=GetHelpSourceDialog(root,'Get Help Source')
115 print dlg.result
116 Button(root,text='Dialog',command=run).pack()
117 root.mainloop()
118
119