PyXR

c:\python24\lib \ idlelib \ configSectionNameDialog.py



0001 """
0002 Dialog that allows user to specify a new config file section name.
0003 Used to get new highlight theme and keybinding set names.
0004 """
0005 from Tkinter import *
0006 import tkMessageBox
0007 
0008 class GetCfgSectionNameDialog(Toplevel):
0009     def __init__(self,parent,title,message,usedNames):
0010         """
0011         message - string, informational message to display
0012         usedNames - list, list of names already in use for validity check
0013         """
0014         Toplevel.__init__(self, parent)
0015         self.configure(borderwidth=5)
0016         self.resizable(height=FALSE,width=FALSE)
0017         self.title(title)
0018         self.transient(parent)
0019         self.grab_set()
0020         self.protocol("WM_DELETE_WINDOW", self.Cancel)
0021         self.parent = parent
0022         self.message=message
0023         self.usedNames=usedNames
0024         self.result=''
0025         self.CreateWidgets()
0026         self.withdraw() #hide while setting geometry
0027         self.update_idletasks()
0028         #needs to be done here so that the winfo_reqwidth is valid
0029         self.messageInfo.config(width=self.frameMain.winfo_reqwidth())
0030         self.geometry("+%d+%d" %
0031             ((parent.winfo_rootx()+((parent.winfo_width()/2)
0032                 -(self.winfo_reqwidth()/2)),
0033               parent.winfo_rooty()+((parent.winfo_height()/2)
0034                 -(self.winfo_reqheight()/2)) )) ) #centre dialog over parent
0035         self.deiconify() #geometry set, unhide
0036         self.wait_window()
0037 
0038     def CreateWidgets(self):
0039         self.name=StringVar(self)
0040         self.fontSize=StringVar(self)
0041         self.frameMain = Frame(self,borderwidth=2,relief=SUNKEN)
0042         self.frameMain.pack(side=TOP,expand=TRUE,fill=BOTH)
0043         self.messageInfo=Message(self.frameMain,anchor=W,justify=LEFT,padx=5,pady=5,
0044                 text=self.message)#,aspect=200)
0045         entryName=Entry(self.frameMain,textvariable=self.name,width=30)
0046         entryName.focus_set()
0047         self.messageInfo.pack(padx=5,pady=5)#,expand=TRUE,fill=BOTH)
0048         entryName.pack(padx=5,pady=5)
0049         frameButtons=Frame(self)
0050         frameButtons.pack(side=BOTTOM,fill=X)
0051         self.buttonOk = Button(frameButtons,text='Ok',
0052                 width=8,command=self.Ok)
0053         self.buttonOk.grid(row=0,column=0,padx=5,pady=5)
0054         self.buttonCancel = Button(frameButtons,text='Cancel',
0055                 width=8,command=self.Cancel)
0056         self.buttonCancel.grid(row=0,column=1,padx=5,pady=5)
0057 
0058     def NameOk(self):
0059         #simple validity check for a sensible
0060         #ConfigParser file section name
0061         nameOk=1
0062         name=self.name.get()
0063         name.strip()
0064         if not name: #no name specified
0065             tkMessageBox.showerror(title='Name Error',
0066                     message='No name specified.', parent=self)
0067             nameOk=0
0068         elif len(name)>30: #name too long
0069             tkMessageBox.showerror(title='Name Error',
0070                     message='Name too long. It should be no more than '+
0071                     '30 characters.', parent=self)
0072             nameOk=0
0073         elif name in self.usedNames:
0074             tkMessageBox.showerror(title='Name Error',
0075                     message='This name is already in use.', parent=self)
0076             nameOk=0
0077         return nameOk
0078 
0079     def Ok(self, event=None):
0080         if self.NameOk():
0081             self.result=self.name.get().strip()
0082             self.destroy()
0083 
0084     def Cancel(self, event=None):
0085         self.result=''
0086         self.destroy()
0087 
0088 if __name__ == '__main__':
0089     #test the dialog
0090     root=Tk()
0091     def run():
0092         keySeq=''
0093         dlg=GetCfgSectionNameDialog(root,'Get Name',
0094                 'The information here should need to be word wrapped. Test.')
0095         print dlg.result
0096     Button(root,text='Dialog',command=run).pack()
0097     root.mainloop()
0098 

Generated by PyXR 0.9.4
SourceForge.net Logo