Steven M. Gava | 813b56e | 2002-01-24 05:59:05 +0000 | [diff] [blame] | 1 | """ |
| 2 | Dialog that allows user to specify a new config file section name. |
| 3 | Used to get new highlight theme and keybinding set names. |
| 4 | """ |
| 5 | from Tkinter import * |
| 6 | import tkMessageBox |
| 7 | |
| 8 | class GetCfgSectionNameDialog(Toplevel): |
| 9 | def __init__(self,parent,title,message,usedNames): |
| 10 | """ |
| 11 | message - string, informational message to display |
| 12 | usedNames - list, list of names already in use for validity check |
| 13 | """ |
| 14 | Toplevel.__init__(self, parent) |
| 15 | self.configure(borderwidth=5) |
| 16 | self.resizable(height=FALSE,width=FALSE) |
| 17 | self.title(title) |
| 18 | self.transient(parent) |
| 19 | self.grab_set() |
| 20 | self.protocol("WM_DELETE_WINDOW", self.Cancel) |
| 21 | self.parent = parent |
| 22 | self.message=message |
| 23 | self.usedNames=usedNames |
| 24 | self.result='' |
| 25 | self.CreateWidgets() |
| 26 | self.withdraw() #hide while setting geometry |
| 27 | self.update_idletasks() |
| 28 | #needs to be done here so that the winfo_reqwidth is valid |
| 29 | self.messageInfo.config(width=self.frameMain.winfo_reqwidth()) |
| 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.name=StringVar(self) |
| 40 | self.fontSize=StringVar(self) |
| 41 | self.frameMain = Frame(self,borderwidth=2,relief=SUNKEN) |
| 42 | self.frameMain.pack(side=TOP,expand=TRUE,fill=BOTH) |
| 43 | self.messageInfo=Message(self.frameMain,anchor=W,justify=LEFT,padx=5,pady=5, |
| 44 | text=self.message)#,aspect=200) |
| 45 | entryName=Entry(self.frameMain,textvariable=self.name,width=30) |
| 46 | entryName.focus_set() |
| 47 | self.messageInfo.pack(padx=5,pady=5)#,expand=TRUE,fill=BOTH) |
| 48 | entryName.pack(padx=5,pady=5) |
| 49 | frameButtons=Frame(self) |
| 50 | frameButtons.pack(side=BOTTOM,fill=X) |
| 51 | self.buttonOk = Button(frameButtons,text='Ok', |
| 52 | width=8,command=self.Ok) |
| 53 | self.buttonOk.grid(row=0,column=0,padx=5,pady=5) |
| 54 | self.buttonCancel = Button(frameButtons,text='Cancel', |
| 55 | width=8,command=self.Cancel) |
| 56 | self.buttonCancel.grid(row=0,column=1,padx=5,pady=5) |
| 57 | |
| 58 | def NameOk(self): |
| 59 | #simple validity check for a sensible |
| 60 | #ConfigParser file section name |
| 61 | nameOk=1 |
| 62 | name=self.name.get() |
| 63 | name.strip() |
| 64 | if not name: #no name specified |
| 65 | tkMessageBox.showerror(title='Name Error', |
| 66 | message='No name specified.') |
| 67 | nameOk=0 |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 68 | elif len(name)>30: #name too long |
Steven M. Gava | 813b56e | 2002-01-24 05:59:05 +0000 | [diff] [blame] | 69 | tkMessageBox.showerror(title='Name Error', |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 70 | message='Name too long. It should be no more than '+ |
| 71 | '30 characters.') |
Steven M. Gava | 813b56e | 2002-01-24 05:59:05 +0000 | [diff] [blame] | 72 | nameOk=0 |
| 73 | elif name in self.usedNames: |
| 74 | tkMessageBox.showerror(title='Name Error', |
| 75 | message='This name is already in use.') |
| 76 | nameOk=0 |
| 77 | return nameOk |
| 78 | |
| 79 | def Ok(self, event=None): |
| 80 | if self.NameOk(): |
Steven M. Gava | 085eb1b | 2002-02-05 04:52:32 +0000 | [diff] [blame] | 81 | self.result=self.name.get().strip() |
Steven M. Gava | 813b56e | 2002-01-24 05:59:05 +0000 | [diff] [blame] | 82 | self.destroy() |
| 83 | |
| 84 | def Cancel(self, event=None): |
| 85 | self.result='' |
| 86 | self.destroy() |
| 87 | |
| 88 | if __name__ == '__main__': |
| 89 | #test the dialog |
| 90 | root=Tk() |
| 91 | def run(): |
| 92 | keySeq='' |
| 93 | dlg=GetCfgSectionNameDialog(root,'Get Name', |
| 94 | 'The information here should need to be word wrapped. Test.') |
| 95 | print dlg.result |
| 96 | Button(root,text='Dialog',command=run).pack() |
| 97 | root.mainloop() |
| 98 | |
| 99 | |