configSectionNameDialog.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. The 'return value' for the dialog, used two placed in configDialog.py,
  5. is the .result attribute set in the Ok and Cancel methods.
  6. """
  7. from Tkinter import *
  8. import tkMessageBox
  9. class GetCfgSectionNameDialog(Toplevel):
  10. def __init__(self, parent, title, message, used_names, _htest=False):
  11. """
  12. message - string, informational message to display
  13. used_names - string collection, names already in use for validity check
  14. _htest - bool, change box location when running htest
  15. """
  16. Toplevel.__init__(self, parent)
  17. self.configure(borderwidth=5)
  18. self.resizable(height=FALSE, width=FALSE)
  19. self.title(title)
  20. self.transient(parent)
  21. self.grab_set()
  22. self.protocol("WM_DELETE_WINDOW", self.Cancel)
  23. self.parent = parent
  24. self.message = message
  25. self.used_names = used_names
  26. self.create_widgets()
  27. self.withdraw() #hide while setting geometry
  28. self.update_idletasks()
  29. #needs to be done here so that the winfo_reqwidth is valid
  30. self.messageInfo.config(width=self.frameMain.winfo_reqwidth())
  31. self.geometry(
  32. "+%d+%d" % (
  33. parent.winfo_rootx() +
  34. (parent.winfo_width()/2 - self.winfo_reqwidth()/2),
  35. parent.winfo_rooty() +
  36. ((parent.winfo_height()/2 - self.winfo_reqheight()/2)
  37. if not _htest else 100)
  38. ) ) #centre dialog over parent (or below htest box)
  39. self.deiconify() #geometry set, unhide
  40. self.wait_window()
  41. def create_widgets(self):
  42. self.name = StringVar(self.parent)
  43. self.fontSize = StringVar(self.parent)
  44. self.frameMain = Frame(self, borderwidth=2, relief=SUNKEN)
  45. self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH)
  46. self.messageInfo = Message(self.frameMain, anchor=W, justify=LEFT,
  47. padx=5, pady=5, text=self.message) #,aspect=200)
  48. entryName = Entry(self.frameMain, textvariable=self.name, width=30)
  49. entryName.focus_set()
  50. self.messageInfo.pack(padx=5, pady=5) #, expand=TRUE, fill=BOTH)
  51. entryName.pack(padx=5, pady=5)
  52. frameButtons = Frame(self, pady=2)
  53. frameButtons.pack(side=BOTTOM)
  54. self.buttonOk = Button(frameButtons, text='Ok',
  55. width=8, command=self.Ok)
  56. self.buttonOk.pack(side=LEFT, padx=5)
  57. self.buttonCancel = Button(frameButtons, text='Cancel',
  58. width=8, command=self.Cancel)
  59. self.buttonCancel.pack(side=RIGHT, padx=5)
  60. def name_ok(self):
  61. ''' After stripping entered name, check that it is a sensible
  62. ConfigParser file section name. Return it if it is, '' if not.
  63. '''
  64. name = self.name.get().strip()
  65. if not name: #no name specified
  66. tkMessageBox.showerror(title='Name Error',
  67. message='No name specified.', parent=self)
  68. elif len(name)>30: #name too long
  69. tkMessageBox.showerror(title='Name Error',
  70. message='Name too long. It should be no more than '+
  71. '30 characters.', parent=self)
  72. name = ''
  73. elif name in self.used_names:
  74. tkMessageBox.showerror(title='Name Error',
  75. message='This name is already in use.', parent=self)
  76. name = ''
  77. return name
  78. def Ok(self, event=None):
  79. name = self.name_ok()
  80. if name:
  81. self.result = name
  82. self.destroy()
  83. def Cancel(self, event=None):
  84. self.result = ''
  85. self.destroy()
  86. if __name__ == '__main__':
  87. import unittest
  88. unittest.main('idlelib.idle_test.test_config_name', verbosity=2, exit=False)
  89. from idlelib.idle_test.htest import run
  90. run(GetCfgSectionNameDialog)