|
1 | | -""" |
2 | | -Dialog that allows user to specify or edit the parameters for a user configured |
3 | | -help source. |
4 | | -""" |
| 1 | +"Dialog to specify or edit the parameters for a user configured help source." |
| 2 | + |
5 | 3 | from Tkinter import * |
6 | 4 | import tkMessageBox |
7 | 5 | import os |
8 | 6 |
|
9 | 7 | class GetHelpSourceDialog(Toplevel): |
10 | | - def __init__(self,parent,title,menuItem='',filePath=''): |
| 8 | + def __init__(self, parent, title, menuItem='', filePath=''): |
11 | 9 | """ |
12 | 10 | menuItem - string, the menu item to edit, if any |
13 | 11 | filePath - string, the help file path to edit, if any |
14 | 12 | """ |
15 | 13 | Toplevel.__init__(self, parent) |
16 | 14 | self.configure(borderwidth=5) |
17 | | - self.resizable(height=FALSE,width=FALSE) |
| 15 | + self.resizable(height=FALSE, width=FALSE) |
18 | 16 | self.title(title) |
19 | 17 | self.transient(parent) |
20 | 18 | self.grab_set() |
21 | 19 | self.protocol("WM_DELETE_WINDOW", self.Cancel) |
22 | 20 | self.parent = parent |
23 | | - self.result=None |
| 21 | + self.result = None |
24 | 22 | self.CreateWidgets() |
25 | 23 | self.menu.set(menuItem) |
26 | 24 | self.path.set(filePath) |
27 | 25 | self.withdraw() #hide while setting geometry |
28 | | - self.update_idletasks() |
29 | 26 | #needs to be done here so that the winfo_reqwidth is valid |
| 27 | + self.update_idletasks() |
| 28 | + #centre dialog over parent: |
30 | 29 | 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 |
| 30 | + ((parent.winfo_rootx() + ((parent.winfo_width()/2) |
| 31 | + -(self.winfo_reqwidth()/2)), |
| 32 | + parent.winfo_rooty() + ((parent.winfo_height()/2) |
| 33 | + -(self.winfo_reqheight()/2))))) |
35 | 34 | self.deiconify() #geometry set, unhide |
| 35 | + self.bind('<Return>', self.Ok) |
36 | 36 | self.wait_window() |
37 | 37 |
|
38 | 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) |
| 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, |
| 47 | + width=30) |
47 | 48 | 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) |
| 49 | + labelPath = Label(self.frameMain, anchor=W, justify=LEFT, |
| 50 | + text='Help File Path:') |
| 51 | + self.entryPath = Entry(self.frameMain, textvariable=self.path, |
| 52 | + width=40) |
51 | 53 | 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) |
| 54 | + labelMenu.pack(anchor=W, padx=5, pady=3) |
| 55 | + self.entryMenu.pack(anchor=W, padx=5, pady=3) |
| 56 | + labelPath.pack(anchor=W, padx=5, pady=3) |
| 57 | + self.entryPath.pack(anchor=W, padx=5, pady=3) |
| 58 | + frameButtons = Frame(self) |
| 59 | + frameButtons.pack(side=BOTTOM, fill=X) |
| 60 | + self.buttonOk = Button(frameButtons, text='OK', |
| 61 | + width=8, default=ACTIVE, command=self.Ok) |
| 62 | + self.buttonOk.grid(row=0, column=0, padx=5,pady=5) |
| 63 | + self.buttonOk.bind('<Return>', self.Ok) |
| 64 | + #self.buttonOk.focus() |
| 65 | + self.buttonCancel = Button(frameButtons, text='Cancel', |
| 66 | + width=8, command=self.Cancel) |
| 67 | + self.buttonCancel.grid(row=0, column=1, padx=5, pady=5) |
64 | 68 |
|
65 | 69 | def MenuOk(self): |
66 | | - #simple validity check for a sensible |
67 | | - #menu item name |
68 | | - menuOk=1 |
69 | | - menu=self.menu.get() |
| 70 | + "Simple validity check for a sensible menu item name" |
| 71 | + menuOk = True |
| 72 | + menu = self.menu.get() |
70 | 73 | menu.strip() |
71 | | - if not menu: #no menu item specified |
| 74 | + if not menu: |
72 | 75 | tkMessageBox.showerror(title='Menu Item Error', |
73 | | - message='No menu item specified.') |
| 76 | + message='No menu item specified', |
| 77 | + parent=self) |
74 | 78 | self.entryMenu.focus_set() |
75 | | - menuOk=0 |
76 | | - elif len(menu)>30: #menu item name too long |
| 79 | + menuOk = False |
| 80 | + elif len(menu) > 30: |
77 | 81 | tkMessageBox.showerror(title='Menu Item Error', |
78 | | - message='Menu item too long. It should be no more '+ |
79 | | - 'than 30 characters.') |
| 82 | + message='Menu item too long:' |
| 83 | + '\nLimit 30 characters.', |
| 84 | + parent=self) |
80 | 85 | self.entryMenu.focus_set() |
81 | | - menuOk=0 |
| 86 | + menuOk = False |
82 | 87 | return menuOk |
83 | 88 |
|
84 | 89 | def PathOk(self): |
85 | | - #simple validity check for menu file path |
86 | | - pathOk=1 |
87 | | - path=self.path.get() |
| 90 | + "Simple validity check for menu file path" |
| 91 | + pathOk = True |
| 92 | + path = self.path.get() |
88 | 93 | path.strip() |
89 | 94 | if not path: #no path specified |
90 | 95 | tkMessageBox.showerror(title='File Path Error', |
91 | | - message='No help file path specified.') |
| 96 | + message='No help file path specified.', |
| 97 | + parent=self) |
92 | 98 | self.entryPath.focus_set() |
93 | | - pathOk=0 |
| 99 | + pathOk = False |
94 | 100 | elif not os.path.exists(path): |
95 | 101 | tkMessageBox.showerror(title='File Path Error', |
96 | | - message='Help file path does not exist.') |
| 102 | + message='Help file path does not exist.', |
| 103 | + parent=self) |
97 | 104 | self.entryPath.focus_set() |
98 | | - pathOk=0 |
| 105 | + pathOk = False |
99 | 106 | return pathOk |
100 | 107 |
|
101 | 108 | 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() |
| 109 | + if self.MenuOk() and self.PathOk(): |
| 110 | + self.result = (self.menu.get().strip(), |
| 111 | + self.path.get().strip()) |
| 112 | + self.destroy() |
106 | 113 |
|
107 | 114 | def Cancel(self, event=None): |
108 | | - self.result=None |
| 115 | + self.result = None |
109 | 116 | self.destroy() |
110 | 117 |
|
111 | 118 | if __name__ == '__main__': |
112 | 119 | #test the dialog |
113 | | - root=Tk() |
| 120 | + root = Tk() |
114 | 121 | def run(): |
115 | | - keySeq='' |
116 | | - dlg=GetHelpSourceDialog(root,'Get Help Source') |
| 122 | + keySeq = '' |
| 123 | + dlg = GetHelpSourceDialog(root, 'Get Help Source') |
117 | 124 | print dlg.result |
118 | | - Button(root,text='Dialog',command=run).pack() |
| 125 | + Button(root,text='Dialog', command=run).pack() |
119 | 126 | root.mainloop() |
0 commit comments