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