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):
|
---|
11 | """
|
---|
12 | message - string, informational message to display
|
---|
13 | used_names - string collection, names already in use for validity check
|
---|
14 | """
|
---|
15 | Toplevel.__init__(self, parent)
|
---|
16 | self.configure(borderwidth=5)
|
---|
17 | self.resizable(height=FALSE, width=FALSE)
|
---|
18 | self.title(title)
|
---|
19 | self.transient(parent)
|
---|
20 | self.grab_set()
|
---|
21 | self.protocol("WM_DELETE_WINDOW", self.Cancel)
|
---|
22 | self.parent = parent
|
---|
23 | self.message = message
|
---|
24 | self.used_names = used_names
|
---|
25 | self.create_widgets()
|
---|
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(
|
---|
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
|
---|
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)
|
---|
58 |
|
---|
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 | if not name: #no name specified
|
---|
65 | tkMessageBox.showerror(title='Name Error',
|
---|
66 | message='No name specified.', parent=self)
|
---|
67 | elif len(name)>30: #name too long
|
---|
68 | tkMessageBox.showerror(title='Name Error',
|
---|
69 | message='Name too long. It should be no more than '+
|
---|
70 | '30 characters.', parent=self)
|
---|
71 | name = ''
|
---|
72 | elif name in self.used_names:
|
---|
73 | tkMessageBox.showerror(title='Name Error',
|
---|
74 | message='This name is already in use.', parent=self)
|
---|
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)
|
---|
88 |
|
---|
89 | # also human test the dialog
|
---|
90 | root = Tk()
|
---|
91 | def run():
|
---|
92 | dlg=GetCfgSectionNameDialog(root,'Get Name',
|
---|
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'})
|
---|
97 | print dlg.result
|
---|
98 | Message(root, text='').pack() # will be needed for oher dialog tests
|
---|
99 | Button(root, text='Click to begin dialog test', command=run).pack()
|
---|
100 | root.mainloop()
|
---|