1 | n/a | """ |
---|
2 | n/a | Dialog for building Tkinter accelerator key bindings |
---|
3 | n/a | """ |
---|
4 | n/a | from tkinter import * |
---|
5 | n/a | from tkinter.ttk import Scrollbar |
---|
6 | n/a | import tkinter.messagebox as tkMessageBox |
---|
7 | n/a | import string |
---|
8 | n/a | import sys |
---|
9 | n/a | |
---|
10 | n/a | class GetKeysDialog(Toplevel): |
---|
11 | n/a | def __init__(self, parent, title, action, currentKeySequences, |
---|
12 | n/a | _htest=False, _utest=False): |
---|
13 | n/a | """ |
---|
14 | n/a | action - string, the name of the virtual event these keys will be |
---|
15 | n/a | mapped to |
---|
16 | n/a | currentKeys - list, a list of all key sequence lists currently mapped |
---|
17 | n/a | to virtual events, for overlap checking |
---|
18 | n/a | _utest - bool, do not wait when running unittest |
---|
19 | n/a | _htest - bool, change box location when running htest |
---|
20 | n/a | """ |
---|
21 | n/a | Toplevel.__init__(self, parent) |
---|
22 | n/a | self.withdraw() #hide while setting geometry |
---|
23 | n/a | self.configure(borderwidth=5) |
---|
24 | n/a | self.resizable(height=FALSE, width=FALSE) |
---|
25 | n/a | self.title(title) |
---|
26 | n/a | self.transient(parent) |
---|
27 | n/a | self.grab_set() |
---|
28 | n/a | self.protocol("WM_DELETE_WINDOW", self.Cancel) |
---|
29 | n/a | self.parent = parent |
---|
30 | n/a | self.action=action |
---|
31 | n/a | self.currentKeySequences = currentKeySequences |
---|
32 | n/a | self.result = '' |
---|
33 | n/a | self.keyString = StringVar(self) |
---|
34 | n/a | self.keyString.set('') |
---|
35 | n/a | self.SetModifiersForPlatform() # set self.modifiers, self.modifier_label |
---|
36 | n/a | self.modifier_vars = [] |
---|
37 | n/a | for modifier in self.modifiers: |
---|
38 | n/a | variable = StringVar(self) |
---|
39 | n/a | variable.set('') |
---|
40 | n/a | self.modifier_vars.append(variable) |
---|
41 | n/a | self.advanced = False |
---|
42 | n/a | self.CreateWidgets() |
---|
43 | n/a | self.LoadFinalKeyList() |
---|
44 | n/a | self.update_idletasks() |
---|
45 | n/a | self.geometry( |
---|
46 | n/a | "+%d+%d" % ( |
---|
47 | n/a | parent.winfo_rootx() + |
---|
48 | n/a | (parent.winfo_width()/2 - self.winfo_reqwidth()/2), |
---|
49 | n/a | parent.winfo_rooty() + |
---|
50 | n/a | ((parent.winfo_height()/2 - self.winfo_reqheight()/2) |
---|
51 | n/a | if not _htest else 150) |
---|
52 | n/a | ) ) #centre dialog over parent (or below htest box) |
---|
53 | n/a | if not _utest: |
---|
54 | n/a | self.deiconify() #geometry set, unhide |
---|
55 | n/a | self.wait_window() |
---|
56 | n/a | |
---|
57 | n/a | def CreateWidgets(self): |
---|
58 | n/a | frameMain = Frame(self,borderwidth=2,relief=SUNKEN) |
---|
59 | n/a | frameMain.pack(side=TOP,expand=TRUE,fill=BOTH) |
---|
60 | n/a | frameButtons=Frame(self) |
---|
61 | n/a | frameButtons.pack(side=BOTTOM,fill=X) |
---|
62 | n/a | self.buttonOK = Button(frameButtons,text='OK', |
---|
63 | n/a | width=8,command=self.OK) |
---|
64 | n/a | self.buttonOK.grid(row=0,column=0,padx=5,pady=5) |
---|
65 | n/a | self.buttonCancel = Button(frameButtons,text='Cancel', |
---|
66 | n/a | width=8,command=self.Cancel) |
---|
67 | n/a | self.buttonCancel.grid(row=0,column=1,padx=5,pady=5) |
---|
68 | n/a | self.frameKeySeqBasic = Frame(frameMain) |
---|
69 | n/a | self.frameKeySeqAdvanced = Frame(frameMain) |
---|
70 | n/a | self.frameControlsBasic = Frame(frameMain) |
---|
71 | n/a | self.frameHelpAdvanced = Frame(frameMain) |
---|
72 | n/a | self.frameKeySeqAdvanced.grid(row=0,column=0,sticky=NSEW,padx=5,pady=5) |
---|
73 | n/a | self.frameKeySeqBasic.grid(row=0,column=0,sticky=NSEW,padx=5,pady=5) |
---|
74 | n/a | self.frameKeySeqBasic.lift() |
---|
75 | n/a | self.frameHelpAdvanced.grid(row=1,column=0,sticky=NSEW,padx=5) |
---|
76 | n/a | self.frameControlsBasic.grid(row=1,column=0,sticky=NSEW,padx=5) |
---|
77 | n/a | self.frameControlsBasic.lift() |
---|
78 | n/a | self.buttonLevel = Button(frameMain,command=self.ToggleLevel, |
---|
79 | n/a | text='Advanced Key Binding Entry >>') |
---|
80 | n/a | self.buttonLevel.grid(row=2,column=0,stick=EW,padx=5,pady=5) |
---|
81 | n/a | labelTitleBasic = Label(self.frameKeySeqBasic, |
---|
82 | n/a | text="New keys for '"+self.action+"' :") |
---|
83 | n/a | labelTitleBasic.pack(anchor=W) |
---|
84 | n/a | labelKeysBasic = Label(self.frameKeySeqBasic,justify=LEFT, |
---|
85 | n/a | textvariable=self.keyString,relief=GROOVE,borderwidth=2) |
---|
86 | n/a | labelKeysBasic.pack(ipadx=5,ipady=5,fill=X) |
---|
87 | n/a | self.modifier_checkbuttons = {} |
---|
88 | n/a | column = 0 |
---|
89 | n/a | for modifier, variable in zip(self.modifiers, self.modifier_vars): |
---|
90 | n/a | label = self.modifier_label.get(modifier, modifier) |
---|
91 | n/a | check=Checkbutton(self.frameControlsBasic, |
---|
92 | n/a | command=self.BuildKeyString, |
---|
93 | n/a | text=label,variable=variable,onvalue=modifier,offvalue='') |
---|
94 | n/a | check.grid(row=0,column=column,padx=2,sticky=W) |
---|
95 | n/a | self.modifier_checkbuttons[modifier] = check |
---|
96 | n/a | column += 1 |
---|
97 | n/a | labelFnAdvice=Label(self.frameControlsBasic,justify=LEFT, |
---|
98 | n/a | text=\ |
---|
99 | n/a | "Select the desired modifier keys\n"+ |
---|
100 | n/a | "above, and the final key from the\n"+ |
---|
101 | n/a | "list on the right.\n\n" + |
---|
102 | n/a | "Use upper case Symbols when using\n" + |
---|
103 | n/a | "the Shift modifier. (Letters will be\n" + |
---|
104 | n/a | "converted automatically.)") |
---|
105 | n/a | labelFnAdvice.grid(row=1,column=0,columnspan=4,padx=2,sticky=W) |
---|
106 | n/a | self.listKeysFinal=Listbox(self.frameControlsBasic,width=15,height=10, |
---|
107 | n/a | selectmode=SINGLE) |
---|
108 | n/a | self.listKeysFinal.bind('<ButtonRelease-1>',self.FinalKeySelected) |
---|
109 | n/a | self.listKeysFinal.grid(row=0,column=4,rowspan=4,sticky=NS) |
---|
110 | n/a | scrollKeysFinal=Scrollbar(self.frameControlsBasic,orient=VERTICAL, |
---|
111 | n/a | command=self.listKeysFinal.yview) |
---|
112 | n/a | self.listKeysFinal.config(yscrollcommand=scrollKeysFinal.set) |
---|
113 | n/a | scrollKeysFinal.grid(row=0,column=5,rowspan=4,sticky=NS) |
---|
114 | n/a | self.buttonClear=Button(self.frameControlsBasic, |
---|
115 | n/a | text='Clear Keys',command=self.ClearKeySeq) |
---|
116 | n/a | self.buttonClear.grid(row=2,column=0,columnspan=4) |
---|
117 | n/a | labelTitleAdvanced = Label(self.frameKeySeqAdvanced,justify=LEFT, |
---|
118 | n/a | text="Enter new binding(s) for '"+self.action+"' :\n"+ |
---|
119 | n/a | "(These bindings will not be checked for validity!)") |
---|
120 | n/a | labelTitleAdvanced.pack(anchor=W) |
---|
121 | n/a | self.entryKeysAdvanced=Entry(self.frameKeySeqAdvanced, |
---|
122 | n/a | textvariable=self.keyString) |
---|
123 | n/a | self.entryKeysAdvanced.pack(fill=X) |
---|
124 | n/a | labelHelpAdvanced=Label(self.frameHelpAdvanced,justify=LEFT, |
---|
125 | n/a | text="Key bindings are specified using Tkinter keysyms as\n"+ |
---|
126 | n/a | "in these samples: <Control-f>, <Shift-F2>, <F12>,\n" |
---|
127 | n/a | "<Control-space>, <Meta-less>, <Control-Alt-Shift-X>.\n" |
---|
128 | n/a | "Upper case is used when the Shift modifier is present!\n\n" + |
---|
129 | n/a | "'Emacs style' multi-keystroke bindings are specified as\n" + |
---|
130 | n/a | "follows: <Control-x><Control-y>, where the first key\n" + |
---|
131 | n/a | "is the 'do-nothing' keybinding.\n\n" + |
---|
132 | n/a | "Multiple separate bindings for one action should be\n"+ |
---|
133 | n/a | "separated by a space, eg., <Alt-v> <Meta-v>." ) |
---|
134 | n/a | labelHelpAdvanced.grid(row=0,column=0,sticky=NSEW) |
---|
135 | n/a | |
---|
136 | n/a | def SetModifiersForPlatform(self): |
---|
137 | n/a | """Determine list of names of key modifiers for this platform. |
---|
138 | n/a | |
---|
139 | n/a | The names are used to build Tk bindings -- it doesn't matter if the |
---|
140 | n/a | keyboard has these keys, it matters if Tk understands them. The |
---|
141 | n/a | order is also important: key binding equality depends on it, so |
---|
142 | n/a | config-keys.def must use the same ordering. |
---|
143 | n/a | """ |
---|
144 | n/a | if sys.platform == "darwin": |
---|
145 | n/a | self.modifiers = ['Shift', 'Control', 'Option', 'Command'] |
---|
146 | n/a | else: |
---|
147 | n/a | self.modifiers = ['Control', 'Alt', 'Shift'] |
---|
148 | n/a | self.modifier_label = {'Control': 'Ctrl'} # short name |
---|
149 | n/a | |
---|
150 | n/a | def ToggleLevel(self): |
---|
151 | n/a | if self.buttonLevel.cget('text')[:8]=='Advanced': |
---|
152 | n/a | self.ClearKeySeq() |
---|
153 | n/a | self.buttonLevel.config(text='<< Basic Key Binding Entry') |
---|
154 | n/a | self.frameKeySeqAdvanced.lift() |
---|
155 | n/a | self.frameHelpAdvanced.lift() |
---|
156 | n/a | self.entryKeysAdvanced.focus_set() |
---|
157 | n/a | self.advanced = True |
---|
158 | n/a | else: |
---|
159 | n/a | self.ClearKeySeq() |
---|
160 | n/a | self.buttonLevel.config(text='Advanced Key Binding Entry >>') |
---|
161 | n/a | self.frameKeySeqBasic.lift() |
---|
162 | n/a | self.frameControlsBasic.lift() |
---|
163 | n/a | self.advanced = False |
---|
164 | n/a | |
---|
165 | n/a | def FinalKeySelected(self,event): |
---|
166 | n/a | self.BuildKeyString() |
---|
167 | n/a | |
---|
168 | n/a | def BuildKeyString(self): |
---|
169 | n/a | keyList = modifiers = self.GetModifiers() |
---|
170 | n/a | finalKey = self.listKeysFinal.get(ANCHOR) |
---|
171 | n/a | if finalKey: |
---|
172 | n/a | finalKey = self.TranslateKey(finalKey, modifiers) |
---|
173 | n/a | keyList.append(finalKey) |
---|
174 | n/a | self.keyString.set('<' + '-'.join(keyList) + '>') |
---|
175 | n/a | |
---|
176 | n/a | def GetModifiers(self): |
---|
177 | n/a | modList = [variable.get() for variable in self.modifier_vars] |
---|
178 | n/a | return [mod for mod in modList if mod] |
---|
179 | n/a | |
---|
180 | n/a | def ClearKeySeq(self): |
---|
181 | n/a | self.listKeysFinal.select_clear(0,END) |
---|
182 | n/a | self.listKeysFinal.yview(MOVETO, '0.0') |
---|
183 | n/a | for variable in self.modifier_vars: |
---|
184 | n/a | variable.set('') |
---|
185 | n/a | self.keyString.set('') |
---|
186 | n/a | |
---|
187 | n/a | def LoadFinalKeyList(self): |
---|
188 | n/a | #these tuples are also available for use in validity checks |
---|
189 | n/a | self.functionKeys=('F1','F2','F2','F4','F5','F6','F7','F8','F9', |
---|
190 | n/a | 'F10','F11','F12') |
---|
191 | n/a | self.alphanumKeys=tuple(string.ascii_lowercase+string.digits) |
---|
192 | n/a | self.punctuationKeys=tuple('~!@#%^&*()_-+={}[]|;:,.<>/?') |
---|
193 | n/a | self.whitespaceKeys=('Tab','Space','Return') |
---|
194 | n/a | self.editKeys=('BackSpace','Delete','Insert') |
---|
195 | n/a | self.moveKeys=('Home','End','Page Up','Page Down','Left Arrow', |
---|
196 | n/a | 'Right Arrow','Up Arrow','Down Arrow') |
---|
197 | n/a | #make a tuple of most of the useful common 'final' keys |
---|
198 | n/a | keys=(self.alphanumKeys+self.punctuationKeys+self.functionKeys+ |
---|
199 | n/a | self.whitespaceKeys+self.editKeys+self.moveKeys) |
---|
200 | n/a | self.listKeysFinal.insert(END, *keys) |
---|
201 | n/a | |
---|
202 | n/a | def TranslateKey(self, key, modifiers): |
---|
203 | n/a | "Translate from keycap symbol to the Tkinter keysym" |
---|
204 | n/a | translateDict = {'Space':'space', |
---|
205 | n/a | '~':'asciitilde','!':'exclam','@':'at','#':'numbersign', |
---|
206 | n/a | '%':'percent','^':'asciicircum','&':'ampersand','*':'asterisk', |
---|
207 | n/a | '(':'parenleft',')':'parenright','_':'underscore','-':'minus', |
---|
208 | n/a | '+':'plus','=':'equal','{':'braceleft','}':'braceright', |
---|
209 | n/a | '[':'bracketleft',']':'bracketright','|':'bar',';':'semicolon', |
---|
210 | n/a | ':':'colon',',':'comma','.':'period','<':'less','>':'greater', |
---|
211 | n/a | '/':'slash','?':'question','Page Up':'Prior','Page Down':'Next', |
---|
212 | n/a | 'Left Arrow':'Left','Right Arrow':'Right','Up Arrow':'Up', |
---|
213 | n/a | 'Down Arrow': 'Down', 'Tab':'Tab'} |
---|
214 | n/a | if key in translateDict: |
---|
215 | n/a | key = translateDict[key] |
---|
216 | n/a | if 'Shift' in modifiers and key in string.ascii_lowercase: |
---|
217 | n/a | key = key.upper() |
---|
218 | n/a | key = 'Key-' + key |
---|
219 | n/a | return key |
---|
220 | n/a | |
---|
221 | n/a | def OK(self, event=None): |
---|
222 | n/a | if self.advanced or self.KeysOK(): # doesn't check advanced string yet |
---|
223 | n/a | self.result=self.keyString.get() |
---|
224 | n/a | self.destroy() |
---|
225 | n/a | |
---|
226 | n/a | def Cancel(self, event=None): |
---|
227 | n/a | self.result='' |
---|
228 | n/a | self.destroy() |
---|
229 | n/a | |
---|
230 | n/a | def KeysOK(self): |
---|
231 | n/a | '''Validity check on user's 'basic' keybinding selection. |
---|
232 | n/a | |
---|
233 | n/a | Doesn't check the string produced by the advanced dialog because |
---|
234 | n/a | 'modifiers' isn't set. |
---|
235 | n/a | |
---|
236 | n/a | ''' |
---|
237 | n/a | keys = self.keyString.get() |
---|
238 | n/a | keys.strip() |
---|
239 | n/a | finalKey = self.listKeysFinal.get(ANCHOR) |
---|
240 | n/a | modifiers = self.GetModifiers() |
---|
241 | n/a | # create a key sequence list for overlap check: |
---|
242 | n/a | keySequence = keys.split() |
---|
243 | n/a | keysOK = False |
---|
244 | n/a | title = 'Key Sequence Error' |
---|
245 | n/a | if not keys: |
---|
246 | n/a | tkMessageBox.showerror(title=title, parent=self, |
---|
247 | n/a | message='No keys specified.') |
---|
248 | n/a | elif not keys.endswith('>'): |
---|
249 | n/a | tkMessageBox.showerror(title=title, parent=self, |
---|
250 | n/a | message='Missing the final Key') |
---|
251 | n/a | elif (not modifiers |
---|
252 | n/a | and finalKey not in self.functionKeys + self.moveKeys): |
---|
253 | n/a | tkMessageBox.showerror(title=title, parent=self, |
---|
254 | n/a | message='No modifier key(s) specified.') |
---|
255 | n/a | elif (modifiers == ['Shift']) \ |
---|
256 | n/a | and (finalKey not in |
---|
257 | n/a | self.functionKeys + self.moveKeys + ('Tab', 'Space')): |
---|
258 | n/a | msg = 'The shift modifier by itself may not be used with'\ |
---|
259 | n/a | ' this key symbol.' |
---|
260 | n/a | tkMessageBox.showerror(title=title, parent=self, message=msg) |
---|
261 | n/a | elif keySequence in self.currentKeySequences: |
---|
262 | n/a | msg = 'This key combination is already in use.' |
---|
263 | n/a | tkMessageBox.showerror(title=title, parent=self, message=msg) |
---|
264 | n/a | else: |
---|
265 | n/a | keysOK = True |
---|
266 | n/a | return keysOK |
---|
267 | n/a | |
---|
268 | n/a | |
---|
269 | n/a | if __name__ == '__main__': |
---|
270 | n/a | from idlelib.idle_test.htest import run |
---|
271 | n/a | run(GetKeysDialog) |
---|