Changeset 391 for python/trunk/Lib/idlelib/configSectionNameDialog.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/idlelib/configSectionNameDialog.py
r2 r391 2 2 Dialog that allows user to specify a new config file section name. 3 3 Used to get new highlight theme and keybinding set names. 4 The 'return value' for the dialog, used two placed in configDialog.py, 5 is the .result attribute set in the Ok and Cancel methods. 4 6 """ 5 7 from Tkinter import * 6 8 import tkMessageBox 7 8 9 class GetCfgSectionNameDialog(Toplevel): 9 def __init__(self, parent,title,message,usedNames):10 def __init__(self, parent, title, message, used_names): 10 11 """ 11 12 message - string, informational message to display 12 used Names - list, list ofnames already in use for validity check13 used_names - string collection, names already in use for validity check 13 14 """ 14 15 Toplevel.__init__(self, parent) 15 16 self.configure(borderwidth=5) 16 self.resizable(height=FALSE, width=FALSE)17 self.resizable(height=FALSE, width=FALSE) 17 18 self.title(title) 18 19 self.transient(parent) … … 20 21 self.protocol("WM_DELETE_WINDOW", self.Cancel) 21 22 self.parent = parent 22 self.message=message 23 self.usedNames=usedNames 24 self.result='' 25 self.CreateWidgets() 26 self.withdraw() #hide while setting geometry 23 self.message = message 24 self.used_names = used_names 25 self.create_widgets() 26 self.withdraw() #hide while setting geometry 27 27 self.update_idletasks() 28 28 #needs to be done here so that the winfo_reqwidth is valid 29 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 30 self.geometry( 31 "+%d+%d" % ( 32 parent.winfo_rootx() + 33 (parent.winfo_width()/2 - self.winfo_reqwidth()/2), 34 parent.winfo_rooty() + 35 (parent.winfo_height()/2 - self.winfo_reqheight()/2) 36 ) ) #centre dialog over parent 37 self.deiconify() #geometry set, unhide 36 38 self.wait_window() 39 def create_widgets(self): 40 self.name = StringVar(self.parent) 41 self.fontSize = StringVar(self.parent) 42 self.frameMain = Frame(self, borderwidth=2, relief=SUNKEN) 43 self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH) 44 self.messageInfo = Message(self.frameMain, anchor=W, justify=LEFT, 45 padx=5, pady=5, text=self.message) #,aspect=200) 46 entryName = Entry(self.frameMain, textvariable=self.name, width=30) 47 entryName.focus_set() 48 self.messageInfo.pack(padx=5, pady=5) #, expand=TRUE, fill=BOTH) 49 entryName.pack(padx=5, pady=5) 50 frameButtons = Frame(self, pady=2) 51 frameButtons.pack(side=BOTTOM) 52 self.buttonOk = Button(frameButtons, text='Ok', 53 width=8, command=self.Ok) 54 self.buttonOk.pack(side=LEFT, padx=5) 55 self.buttonCancel = Button(frameButtons, text='Cancel', 56 width=8, command=self.Cancel) 57 self.buttonCancel.pack(side=RIGHT, padx=5) 37 58 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() 59 def name_ok(self): 60 ''' After stripping entered name, check that it is a sensible 61 ConfigParser file section name. Return it if it is, '' if not. 62 ''' 63 name = self.name.get().strip() 64 64 if not name: #no name specified 65 65 tkMessageBox.showerror(title='Name Error', 66 66 message='No name specified.', parent=self) 67 nameOk=068 67 elif len(name)>30: #name too long 69 68 tkMessageBox.showerror(title='Name Error', 70 69 message='Name too long. It should be no more than '+ 71 70 '30 characters.', parent=self) 72 name Ok=073 elif name in self.used Names:71 name = '' 72 elif name in self.used_names: 74 73 tkMessageBox.showerror(title='Name Error', 75 74 message='This name is already in use.', parent=self) 76 nameOk=0 77 return nameOk 75 name = '' 76 return name 77 def Ok(self, event=None): 78 name = self.name_ok() 79 if name: 80 self.result = name 81 self.destroy() 82 def Cancel(self, event=None): 83 self.result = '' 84 self.destroy() 85 if __name__ == '__main__': 86 import unittest 87 unittest.main('idlelib.idle_test.test_config_name', verbosity=2, exit=False) 78 88 79 def Ok(self, event=None): 80 if self.NameOk(): 81 self.result=self.name.get().strip() 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() 89 # also human test the dialog 90 root = Tk() 91 91 def run(): 92 keySeq=''93 92 dlg=GetCfgSectionNameDialog(root,'Get Name', 94 'The information here should need to be word wrapped. Test.') 93 "After the text entered with [Ok] is stripped, <nothing>, " 94 "'abc', or more that 30 chars are errors. " 95 "Close with a valid entry (printed), [Cancel], or [X]", 96 {'abc'}) 95 97 print dlg.result 96 Button(root,text='Dialog',command=run).pack() 98 Message(root, text='').pack() # will be needed for oher dialog tests 99 Button(root, text='Click to begin dialog test', command=run).pack() 97 100 root.mainloop()
Note:
See TracChangeset
for help on using the changeset viewer.