10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 20a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDialog that allows user to specify a new config file section name. 30a8c90248264a8b26970b4473770bcc3df8515fJosh GaoUsed to get new highlight theme and keybinding set names. 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom Tkinter import * 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport tkMessageBox 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass GetCfgSectionNameDialog(Toplevel): 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self,parent,title,message,usedNames): 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao message - string, informational message to display 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao usedNames - list, list of names already in use for validity check 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Toplevel.__init__(self, parent) 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.configure(borderwidth=5) 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.resizable(height=FALSE,width=FALSE) 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.title(title) 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.transient(parent) 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.grab_set() 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.protocol("WM_DELETE_WINDOW", self.Cancel) 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.parent = parent 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.message=message 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.usedNames=usedNames 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.result='' 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.CreateWidgets() 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.withdraw() #hide while setting geometry 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.update_idletasks() 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao #needs to be done here so that the winfo_reqwidth is valid 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.messageInfo.config(width=self.frameMain.winfo_reqwidth()) 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.geometry("+%d+%d" % 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ((parent.winfo_rootx()+((parent.winfo_width()/2) 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -(self.winfo_reqwidth()/2)), 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao parent.winfo_rooty()+((parent.winfo_height()/2) 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -(self.winfo_reqheight()/2)) )) ) #centre dialog over parent 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.deiconify() #geometry set, unhide 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.wait_window() 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def CreateWidgets(self): 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.name=StringVar(self) 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fontSize=StringVar(self) 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.frameMain = Frame(self,borderwidth=2,relief=SUNKEN) 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.frameMain.pack(side=TOP,expand=TRUE,fill=BOTH) 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.messageInfo=Message(self.frameMain,anchor=W,justify=LEFT,padx=5,pady=5, 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text=self.message)#,aspect=200) 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao entryName=Entry(self.frameMain,textvariable=self.name,width=30) 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao entryName.focus_set() 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.messageInfo.pack(padx=5,pady=5)#,expand=TRUE,fill=BOTH) 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao entryName.pack(padx=5,pady=5) 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao frameButtons=Frame(self) 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao frameButtons.pack(side=BOTTOM,fill=X) 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.buttonOk = Button(frameButtons,text='Ok', 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao width=8,command=self.Ok) 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.buttonOk.grid(row=0,column=0,padx=5,pady=5) 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.buttonCancel = Button(frameButtons,text='Cancel', 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao width=8,command=self.Cancel) 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.buttonCancel.grid(row=0,column=1,padx=5,pady=5) 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def NameOk(self): 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao #simple validity check for a sensible 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao #ConfigParser file section name 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao nameOk=1 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao name=self.name.get() 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao name.strip() 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not name: #no name specified 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tkMessageBox.showerror(title='Name Error', 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao message='No name specified.', parent=self) 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao nameOk=0 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif len(name)>30: #name too long 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tkMessageBox.showerror(title='Name Error', 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao message='Name too long. It should be no more than '+ 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '30 characters.', parent=self) 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao nameOk=0 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif name in self.usedNames: 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tkMessageBox.showerror(title='Name Error', 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao message='This name is already in use.', parent=self) 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao nameOk=0 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return nameOk 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def Ok(self, event=None): 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.NameOk(): 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.result=self.name.get().strip() 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.destroy() 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def Cancel(self, event=None): 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.result='' 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.destroy() 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == '__main__': 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao #test the dialog 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao root=Tk() 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def run(): 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao keySeq='' 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dlg=GetCfgSectionNameDialog(root,'Get Name', 940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'The information here should need to be word wrapped. Test.') 950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print dlg.result 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Button(root,text='Dialog',command=run).pack() 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao root.mainloop() 98