mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
bpo-35598: IDLE: Update config_key.py with PEP8 names (GH-11330)
A few other changes make the code easier to follow.
This commit is contained in:
parent
0133f9fc9e
commit
55698cc395
4 changed files with 214 additions and 184 deletions
|
|
@ -13,39 +13,42 @@ class GetKeysDialog(Toplevel):
|
||||||
# Dialog title for invalid key sequence
|
# Dialog title for invalid key sequence
|
||||||
keyerror_title = 'Key Sequence Error'
|
keyerror_title = 'Key Sequence Error'
|
||||||
|
|
||||||
def __init__(self, parent, title, action, currentKeySequences,
|
def __init__(self, parent, title, action, current_key_sequences,
|
||||||
*, _htest=False, _utest=False):
|
*, _htest=False, _utest=False):
|
||||||
"""
|
"""
|
||||||
|
parent - parent of this dialog
|
||||||
|
title - string which is the title of the popup dialog
|
||||||
action - string, the name of the virtual event these keys will be
|
action - string, the name of the virtual event these keys will be
|
||||||
mapped to
|
mapped to
|
||||||
currentKeys - list, a list of all key sequence lists currently mapped
|
current_key_sequences - list, a list of all key sequence lists
|
||||||
to virtual events, for overlap checking
|
currently mapped to virtual events, for overlap checking
|
||||||
_utest - bool, do not wait when running unittest
|
|
||||||
_htest - bool, change box location when running htest
|
_htest - bool, change box location when running htest
|
||||||
|
_utest - bool, do not wait when running unittest
|
||||||
"""
|
"""
|
||||||
Toplevel.__init__(self, parent)
|
Toplevel.__init__(self, parent)
|
||||||
self.withdraw() #hide while setting geometry
|
self.withdraw() # Hide while setting geometry.
|
||||||
self.configure(borderwidth=5)
|
self.configure(borderwidth=5)
|
||||||
self.resizable(height=FALSE, width=FALSE)
|
self.resizable(height=False, width=False)
|
||||||
self.title(title)
|
self.title(title)
|
||||||
self.transient(parent)
|
self.transient(parent)
|
||||||
self.grab_set()
|
self.grab_set()
|
||||||
self.protocol("WM_DELETE_WINDOW", self.Cancel)
|
self.protocol("WM_DELETE_WINDOW", self.cancel)
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.action=action
|
self.action = action
|
||||||
self.currentKeySequences = currentKeySequences
|
self.current_key_sequences = current_key_sequences
|
||||||
self.result = ''
|
self.result = ''
|
||||||
self.keyString = StringVar(self)
|
self.key_string = StringVar(self)
|
||||||
self.keyString.set('')
|
self.key_string.set('')
|
||||||
self.SetModifiersForPlatform() # set self.modifiers, self.modifier_label
|
# Set self.modifiers, self.modifier_label.
|
||||||
|
self.set_modifiers_for_platform()
|
||||||
self.modifier_vars = []
|
self.modifier_vars = []
|
||||||
for modifier in self.modifiers:
|
for modifier in self.modifiers:
|
||||||
variable = StringVar(self)
|
variable = StringVar(self)
|
||||||
variable.set('')
|
variable.set('')
|
||||||
self.modifier_vars.append(variable)
|
self.modifier_vars.append(variable)
|
||||||
self.advanced = False
|
self.advanced = False
|
||||||
self.CreateWidgets()
|
self.create_widgets()
|
||||||
self.LoadFinalKeyList()
|
self.load_final_key_list()
|
||||||
self.update_idletasks()
|
self.update_idletasks()
|
||||||
self.geometry(
|
self.geometry(
|
||||||
"+%d+%d" % (
|
"+%d+%d" % (
|
||||||
|
|
@ -54,83 +57,99 @@ class GetKeysDialog(Toplevel):
|
||||||
parent.winfo_rooty() +
|
parent.winfo_rooty() +
|
||||||
((parent.winfo_height()/2 - self.winfo_reqheight()/2)
|
((parent.winfo_height()/2 - self.winfo_reqheight()/2)
|
||||||
if not _htest else 150)
|
if not _htest else 150)
|
||||||
) ) #centre dialog over parent (or below htest box)
|
) ) # Center dialog over parent (or below htest box).
|
||||||
if not _utest:
|
if not _utest:
|
||||||
self.deiconify() #geometry set, unhide
|
self.deiconify() # Geometry set, unhide.
|
||||||
self.wait_window()
|
self.wait_window()
|
||||||
|
|
||||||
def showerror(self, *args, **kwargs):
|
def showerror(self, *args, **kwargs):
|
||||||
# Make testing easier. Replace in #30751.
|
# Make testing easier. Replace in #30751.
|
||||||
messagebox.showerror(*args, **kwargs)
|
messagebox.showerror(*args, **kwargs)
|
||||||
|
|
||||||
def CreateWidgets(self):
|
def create_widgets(self):
|
||||||
frameMain = Frame(self,borderwidth=2,relief=SUNKEN)
|
frame = Frame(self, borderwidth=2, relief=SUNKEN)
|
||||||
frameMain.pack(side=TOP,expand=TRUE,fill=BOTH)
|
frame.pack(side=TOP, expand=True, fill=BOTH)
|
||||||
frameButtons=Frame(self)
|
|
||||||
frameButtons.pack(side=BOTTOM,fill=X)
|
frame_buttons = Frame(self)
|
||||||
self.buttonOK = Button(frameButtons,text='OK',
|
frame_buttons.pack(side=BOTTOM, fill=X)
|
||||||
width=8,command=self.OK)
|
|
||||||
self.buttonOK.grid(row=0,column=0,padx=5,pady=5)
|
self.button_ok = Button(frame_buttons, text='OK',
|
||||||
self.buttonCancel = Button(frameButtons,text='Cancel',
|
width=8, command=self.ok)
|
||||||
width=8,command=self.Cancel)
|
self.button_ok.grid(row=0, column=0, padx=5, pady=5)
|
||||||
self.buttonCancel.grid(row=0,column=1,padx=5,pady=5)
|
self.button_cancel = Button(frame_buttons, text='Cancel',
|
||||||
self.frameKeySeqBasic = Frame(frameMain)
|
width=8, command=self.cancel)
|
||||||
self.frameKeySeqAdvanced = Frame(frameMain)
|
self.button_cancel.grid(row=0, column=1, padx=5, pady=5)
|
||||||
self.frameControlsBasic = Frame(frameMain)
|
|
||||||
self.frameHelpAdvanced = Frame(frameMain)
|
# Basic entry key sequence.
|
||||||
self.frameKeySeqAdvanced.grid(row=0,column=0,sticky=NSEW,padx=5,pady=5)
|
self.frame_keyseq_basic = Frame(frame)
|
||||||
self.frameKeySeqBasic.grid(row=0,column=0,sticky=NSEW,padx=5,pady=5)
|
self.frame_keyseq_basic.grid(row=0, column=0, sticky=NSEW,
|
||||||
self.frameKeySeqBasic.lift()
|
padx=5, pady=5)
|
||||||
self.frameHelpAdvanced.grid(row=1,column=0,sticky=NSEW,padx=5)
|
basic_title = Label(self.frame_keyseq_basic,
|
||||||
self.frameControlsBasic.grid(row=1,column=0,sticky=NSEW,padx=5)
|
text=f"New keys for '{self.action}' :")
|
||||||
self.frameControlsBasic.lift()
|
basic_title.pack(anchor=W)
|
||||||
self.buttonLevel = Button(frameMain,command=self.ToggleLevel,
|
|
||||||
text='Advanced Key Binding Entry >>')
|
basic_keys = Label(self.frame_keyseq_basic, justify=LEFT,
|
||||||
self.buttonLevel.grid(row=2,column=0,stick=EW,padx=5,pady=5)
|
textvariable=self.key_string, relief=GROOVE,
|
||||||
labelTitleBasic = Label(self.frameKeySeqBasic,
|
borderwidth=2)
|
||||||
text="New keys for '"+self.action+"' :")
|
basic_keys.pack(ipadx=5, ipady=5, fill=X)
|
||||||
labelTitleBasic.pack(anchor=W)
|
|
||||||
labelKeysBasic = Label(self.frameKeySeqBasic,justify=LEFT,
|
# Basic entry controls.
|
||||||
textvariable=self.keyString,relief=GROOVE,borderwidth=2)
|
self.frame_controls_basic = Frame(frame)
|
||||||
labelKeysBasic.pack(ipadx=5,ipady=5,fill=X)
|
self.frame_controls_basic.grid(row=1, column=0, sticky=NSEW, padx=5)
|
||||||
|
|
||||||
|
# Basic entry modifiers.
|
||||||
self.modifier_checkbuttons = {}
|
self.modifier_checkbuttons = {}
|
||||||
column = 0
|
column = 0
|
||||||
for modifier, variable in zip(self.modifiers, self.modifier_vars):
|
for modifier, variable in zip(self.modifiers, self.modifier_vars):
|
||||||
label = self.modifier_label.get(modifier, modifier)
|
label = self.modifier_label.get(modifier, modifier)
|
||||||
check=Checkbutton(self.frameControlsBasic,
|
check = Checkbutton(self.frame_controls_basic,
|
||||||
command=self.BuildKeyString,
|
command=self.build_key_string, text=label,
|
||||||
text=label,variable=variable,onvalue=modifier,offvalue='')
|
variable=variable, onvalue=modifier, offvalue='')
|
||||||
check.grid(row=0,column=column,padx=2,sticky=W)
|
check.grid(row=0, column=column, padx=2, sticky=W)
|
||||||
self.modifier_checkbuttons[modifier] = check
|
self.modifier_checkbuttons[modifier] = check
|
||||||
column += 1
|
column += 1
|
||||||
labelFnAdvice=Label(self.frameControlsBasic,justify=LEFT,
|
|
||||||
text=\
|
# Basic entry help text.
|
||||||
"Select the desired modifier keys\n"+
|
help_basic = Label(self.frame_controls_basic, justify=LEFT,
|
||||||
"above, and the final key from the\n"+
|
text="Select the desired modifier keys\n"+
|
||||||
"list on the right.\n\n" +
|
"above, and the final key from the\n"+
|
||||||
"Use upper case Symbols when using\n" +
|
"list on the right.\n\n" +
|
||||||
"the Shift modifier. (Letters will be\n" +
|
"Use upper case Symbols when using\n" +
|
||||||
"converted automatically.)")
|
"the Shift modifier. (Letters will be\n" +
|
||||||
labelFnAdvice.grid(row=1,column=0,columnspan=4,padx=2,sticky=W)
|
"converted automatically.)")
|
||||||
self.listKeysFinal=Listbox(self.frameControlsBasic,width=15,height=10,
|
help_basic.grid(row=1, column=0, columnspan=4, padx=2, sticky=W)
|
||||||
selectmode=SINGLE)
|
|
||||||
self.listKeysFinal.bind('<ButtonRelease-1>',self.FinalKeySelected)
|
# Basic entry key list.
|
||||||
self.listKeysFinal.grid(row=0,column=4,rowspan=4,sticky=NS)
|
self.list_keys_final = Listbox(self.frame_controls_basic, width=15,
|
||||||
scrollKeysFinal=Scrollbar(self.frameControlsBasic,orient=VERTICAL,
|
height=10, selectmode=SINGLE)
|
||||||
command=self.listKeysFinal.yview)
|
self.list_keys_final.bind('<ButtonRelease-1>', self.final_key_selected)
|
||||||
self.listKeysFinal.config(yscrollcommand=scrollKeysFinal.set)
|
self.list_keys_final.grid(row=0, column=4, rowspan=4, sticky=NS)
|
||||||
scrollKeysFinal.grid(row=0,column=5,rowspan=4,sticky=NS)
|
scroll_keys_final = Scrollbar(self.frame_controls_basic,
|
||||||
self.buttonClear=Button(self.frameControlsBasic,
|
orient=VERTICAL,
|
||||||
text='Clear Keys',command=self.ClearKeySeq)
|
command=self.list_keys_final.yview)
|
||||||
self.buttonClear.grid(row=2,column=0,columnspan=4)
|
self.list_keys_final.config(yscrollcommand=scroll_keys_final.set)
|
||||||
labelTitleAdvanced = Label(self.frameKeySeqAdvanced,justify=LEFT,
|
scroll_keys_final.grid(row=0, column=5, rowspan=4, sticky=NS)
|
||||||
text="Enter new binding(s) for '"+self.action+"' :\n"+
|
self.button_clear = Button(self.frame_controls_basic,
|
||||||
"(These bindings will not be checked for validity!)")
|
text='Clear Keys',
|
||||||
labelTitleAdvanced.pack(anchor=W)
|
command=self.clear_key_seq)
|
||||||
self.entryKeysAdvanced=Entry(self.frameKeySeqAdvanced,
|
self.button_clear.grid(row=2, column=0, columnspan=4)
|
||||||
textvariable=self.keyString)
|
|
||||||
self.entryKeysAdvanced.pack(fill=X)
|
# Advanced entry key sequence.
|
||||||
labelHelpAdvanced=Label(self.frameHelpAdvanced,justify=LEFT,
|
self.frame_keyseq_advanced = Frame(frame)
|
||||||
|
self.frame_keyseq_advanced.grid(row=0, column=0, sticky=NSEW,
|
||||||
|
padx=5, pady=5)
|
||||||
|
advanced_title = Label(self.frame_keyseq_advanced, justify=LEFT,
|
||||||
|
text=f"Enter new binding(s) for '{self.action}' :\n" +
|
||||||
|
"(These bindings will not be checked for validity!)")
|
||||||
|
advanced_title.pack(anchor=W)
|
||||||
|
self.advanced_keys = Entry(self.frame_keyseq_advanced,
|
||||||
|
textvariable=self.key_string)
|
||||||
|
self.advanced_keys.pack(fill=X)
|
||||||
|
|
||||||
|
# Advanced entry help text.
|
||||||
|
self.frame_help_advanced = Frame(frame)
|
||||||
|
self.frame_help_advanced.grid(row=1, column=0, sticky=NSEW, padx=5)
|
||||||
|
help_advanced = Label(self.frame_help_advanced, justify=LEFT,
|
||||||
text="Key bindings are specified using Tkinter keysyms as\n"+
|
text="Key bindings are specified using Tkinter keysyms as\n"+
|
||||||
"in these samples: <Control-f>, <Shift-F2>, <F12>,\n"
|
"in these samples: <Control-f>, <Shift-F2>, <F12>,\n"
|
||||||
"<Control-space>, <Meta-less>, <Control-Alt-Shift-X>.\n"
|
"<Control-space>, <Meta-less>, <Control-Alt-Shift-X>.\n"
|
||||||
|
|
@ -140,13 +159,19 @@ class GetKeysDialog(Toplevel):
|
||||||
"is the 'do-nothing' keybinding.\n\n" +
|
"is the 'do-nothing' keybinding.\n\n" +
|
||||||
"Multiple separate bindings for one action should be\n"+
|
"Multiple separate bindings for one action should be\n"+
|
||||||
"separated by a space, eg., <Alt-v> <Meta-v>." )
|
"separated by a space, eg., <Alt-v> <Meta-v>." )
|
||||||
labelHelpAdvanced.grid(row=0,column=0,sticky=NSEW)
|
help_advanced.grid(row=0, column=0, sticky=NSEW)
|
||||||
|
|
||||||
def SetModifiersForPlatform(self):
|
# Switch between basic and advanced.
|
||||||
|
self.button_level = Button(frame, command=self.toggle_level,
|
||||||
|
text='<< Basic Key Binding Entry')
|
||||||
|
self.button_level.grid(row=2, column=0, stick=EW, padx=5, pady=5)
|
||||||
|
self.toggle_level()
|
||||||
|
|
||||||
|
def set_modifiers_for_platform(self):
|
||||||
"""Determine list of names of key modifiers for this platform.
|
"""Determine list of names of key modifiers for this platform.
|
||||||
|
|
||||||
The names are used to build Tk bindings -- it doesn't matter if the
|
The names are used to build Tk bindings -- it doesn't matter if the
|
||||||
keyboard has these keys, it matters if Tk understands them. The
|
keyboard has these keys; it matters if Tk understands them. The
|
||||||
order is also important: key binding equality depends on it, so
|
order is also important: key binding equality depends on it, so
|
||||||
config-keys.def must use the same ordering.
|
config-keys.def must use the same ordering.
|
||||||
"""
|
"""
|
||||||
|
|
@ -154,118 +179,124 @@ class GetKeysDialog(Toplevel):
|
||||||
self.modifiers = ['Shift', 'Control', 'Option', 'Command']
|
self.modifiers = ['Shift', 'Control', 'Option', 'Command']
|
||||||
else:
|
else:
|
||||||
self.modifiers = ['Control', 'Alt', 'Shift']
|
self.modifiers = ['Control', 'Alt', 'Shift']
|
||||||
self.modifier_label = {'Control': 'Ctrl'} # short name
|
self.modifier_label = {'Control': 'Ctrl'} # Short name.
|
||||||
|
|
||||||
def ToggleLevel(self):
|
def toggle_level(self):
|
||||||
if self.buttonLevel.cget('text')[:8]=='Advanced':
|
"Toggle between basic and advanced keys."
|
||||||
self.ClearKeySeq()
|
if self.button_level.cget('text').startswith('Advanced'):
|
||||||
self.buttonLevel.config(text='<< Basic Key Binding Entry')
|
self.clear_key_seq()
|
||||||
self.frameKeySeqAdvanced.lift()
|
self.button_level.config(text='<< Basic Key Binding Entry')
|
||||||
self.frameHelpAdvanced.lift()
|
self.frame_keyseq_advanced.lift()
|
||||||
self.entryKeysAdvanced.focus_set()
|
self.frame_help_advanced.lift()
|
||||||
|
self.advanced_keys.focus_set()
|
||||||
self.advanced = True
|
self.advanced = True
|
||||||
else:
|
else:
|
||||||
self.ClearKeySeq()
|
self.clear_key_seq()
|
||||||
self.buttonLevel.config(text='Advanced Key Binding Entry >>')
|
self.button_level.config(text='Advanced Key Binding Entry >>')
|
||||||
self.frameKeySeqBasic.lift()
|
self.frame_keyseq_basic.lift()
|
||||||
self.frameControlsBasic.lift()
|
self.frame_controls_basic.lift()
|
||||||
self.advanced = False
|
self.advanced = False
|
||||||
|
|
||||||
def FinalKeySelected(self,event):
|
def final_key_selected(self, event):
|
||||||
self.BuildKeyString()
|
"Handler for clicking on key in basic settings list."
|
||||||
|
self.build_key_string()
|
||||||
|
|
||||||
def BuildKeyString(self):
|
def build_key_string(self):
|
||||||
keyList = modifiers = self.GetModifiers()
|
"Create formatted string of modifiers plus the key."
|
||||||
finalKey = self.listKeysFinal.get(ANCHOR)
|
keylist = modifiers = self.get_modifiers()
|
||||||
if finalKey:
|
final_key = self.list_keys_final.get(ANCHOR)
|
||||||
finalKey = self.TranslateKey(finalKey, modifiers)
|
if final_key:
|
||||||
keyList.append(finalKey)
|
final_key = self.translate_key(final_key, modifiers)
|
||||||
self.keyString.set('<' + '-'.join(keyList) + '>')
|
keylist.append(final_key)
|
||||||
|
self.key_string.set(f"<{'-'.join(keylist)}>")
|
||||||
|
|
||||||
def GetModifiers(self):
|
def get_modifiers(self):
|
||||||
modList = [variable.get() for variable in self.modifier_vars]
|
"Return ordered list of modifiers that have been selected."
|
||||||
return [mod for mod in modList if mod]
|
mod_list = [variable.get() for variable in self.modifier_vars]
|
||||||
|
return [mod for mod in mod_list if mod]
|
||||||
|
|
||||||
def ClearKeySeq(self):
|
def clear_key_seq(self):
|
||||||
self.listKeysFinal.select_clear(0,END)
|
"Clear modifiers and keys selection."
|
||||||
self.listKeysFinal.yview(MOVETO, '0.0')
|
self.list_keys_final.select_clear(0, END)
|
||||||
|
self.list_keys_final.yview(MOVETO, '0.0')
|
||||||
for variable in self.modifier_vars:
|
for variable in self.modifier_vars:
|
||||||
variable.set('')
|
variable.set('')
|
||||||
self.keyString.set('')
|
self.key_string.set('')
|
||||||
|
|
||||||
def LoadFinalKeyList(self):
|
def load_final_key_list(self):
|
||||||
#these tuples are also available for use in validity checks
|
"Populate listbox of available keys."
|
||||||
self.functionKeys=('F1','F2','F3','F4','F5','F6','F7','F8','F9',
|
# These tuples are also available for use in validity checks.
|
||||||
'F10','F11','F12')
|
self.function_keys = ('F1', 'F2' ,'F3' ,'F4' ,'F5' ,'F6',
|
||||||
self.alphanumKeys=tuple(string.ascii_lowercase+string.digits)
|
'F7', 'F8' ,'F9' ,'F10' ,'F11' ,'F12')
|
||||||
self.punctuationKeys=tuple('~!@#%^&*()_-+={}[]|;:,.<>/?')
|
self.alphanum_keys = tuple(string.ascii_lowercase + string.digits)
|
||||||
self.whitespaceKeys=('Tab','Space','Return')
|
self.punctuation_keys = tuple('~!@#%^&*()_-+={}[]|;:,.<>/?')
|
||||||
self.editKeys=('BackSpace','Delete','Insert')
|
self.whitespace_keys = ('Tab', 'Space', 'Return')
|
||||||
self.moveKeys=('Home','End','Page Up','Page Down','Left Arrow',
|
self.edit_keys = ('BackSpace', 'Delete', 'Insert')
|
||||||
'Right Arrow','Up Arrow','Down Arrow')
|
self.move_keys = ('Home', 'End', 'Page Up', 'Page Down', 'Left Arrow',
|
||||||
#make a tuple of most of the useful common 'final' keys
|
'Right Arrow', 'Up Arrow', 'Down Arrow')
|
||||||
keys=(self.alphanumKeys+self.punctuationKeys+self.functionKeys+
|
# Make a tuple of most of the useful common 'final' keys.
|
||||||
self.whitespaceKeys+self.editKeys+self.moveKeys)
|
keys = (self.alphanum_keys + self.punctuation_keys + self.function_keys +
|
||||||
self.listKeysFinal.insert(END, *keys)
|
self.whitespace_keys + self.edit_keys + self.move_keys)
|
||||||
|
self.list_keys_final.insert(END, *keys)
|
||||||
|
|
||||||
def TranslateKey(self, key, modifiers):
|
@staticmethod
|
||||||
"Translate from keycap symbol to the Tkinter keysym"
|
def translate_key(key, modifiers):
|
||||||
translateDict = {'Space':'space',
|
"Translate from keycap symbol to the Tkinter keysym."
|
||||||
'~':'asciitilde','!':'exclam','@':'at','#':'numbersign',
|
translate_dict = {'Space':'space',
|
||||||
'%':'percent','^':'asciicircum','&':'ampersand','*':'asterisk',
|
'~':'asciitilde', '!':'exclam', '@':'at', '#':'numbersign',
|
||||||
'(':'parenleft',')':'parenright','_':'underscore','-':'minus',
|
'%':'percent', '^':'asciicircum', '&':'ampersand',
|
||||||
'+':'plus','=':'equal','{':'braceleft','}':'braceright',
|
'*':'asterisk', '(':'parenleft', ')':'parenright',
|
||||||
'[':'bracketleft',']':'bracketright','|':'bar',';':'semicolon',
|
'_':'underscore', '-':'minus', '+':'plus', '=':'equal',
|
||||||
':':'colon',',':'comma','.':'period','<':'less','>':'greater',
|
'{':'braceleft', '}':'braceright',
|
||||||
'/':'slash','?':'question','Page Up':'Prior','Page Down':'Next',
|
'[':'bracketleft', ']':'bracketright', '|':'bar',
|
||||||
'Left Arrow':'Left','Right Arrow':'Right','Up Arrow':'Up',
|
';':'semicolon', ':':'colon', ',':'comma', '.':'period',
|
||||||
'Down Arrow': 'Down', 'Tab':'Tab'}
|
'<':'less', '>':'greater', '/':'slash', '?':'question',
|
||||||
if key in translateDict:
|
'Page Up':'Prior', 'Page Down':'Next',
|
||||||
key = translateDict[key]
|
'Left Arrow':'Left', 'Right Arrow':'Right',
|
||||||
|
'Up Arrow':'Up', 'Down Arrow': 'Down', 'Tab':'Tab'}
|
||||||
|
if key in translate_dict:
|
||||||
|
key = translate_dict[key]
|
||||||
if 'Shift' in modifiers and key in string.ascii_lowercase:
|
if 'Shift' in modifiers and key in string.ascii_lowercase:
|
||||||
key = key.upper()
|
key = key.upper()
|
||||||
key = 'Key-' + key
|
return f'Key-{key}'
|
||||||
return key
|
|
||||||
|
|
||||||
def OK(self, event=None):
|
def ok(self, event=None):
|
||||||
keys = self.keyString.get().strip()
|
keys = self.key_string.get().strip()
|
||||||
if not keys:
|
if not keys:
|
||||||
self.showerror(title=self.keyerror_title, parent=self,
|
self.showerror(title=self.keyerror_title, parent=self,
|
||||||
message="No key specified.")
|
message="No key specified.")
|
||||||
return
|
return
|
||||||
if (self.advanced or self.KeysOK(keys)) and self.bind_ok(keys):
|
if (self.advanced or self.keys_ok(keys)) and self.bind_ok(keys):
|
||||||
self.result = keys
|
self.result = keys
|
||||||
self.grab_release()
|
self.grab_release()
|
||||||
self.destroy()
|
self.destroy()
|
||||||
|
|
||||||
def Cancel(self, event=None):
|
def cancel(self, event=None):
|
||||||
self.result=''
|
self.result = ''
|
||||||
self.grab_release()
|
self.grab_release()
|
||||||
self.destroy()
|
self.destroy()
|
||||||
|
|
||||||
def KeysOK(self, keys):
|
def keys_ok(self, keys):
|
||||||
'''Validity check on user's 'basic' keybinding selection.
|
"""Validity check on user's 'basic' keybinding selection.
|
||||||
|
|
||||||
Doesn't check the string produced by the advanced dialog because
|
Doesn't check the string produced by the advanced dialog because
|
||||||
'modifiers' isn't set.
|
'modifiers' isn't set.
|
||||||
|
"""
|
||||||
'''
|
final_key = self.list_keys_final.get(ANCHOR)
|
||||||
finalKey = self.listKeysFinal.get(ANCHOR)
|
modifiers = self.get_modifiers()
|
||||||
modifiers = self.GetModifiers()
|
|
||||||
keysOK = False
|
|
||||||
title = self.keyerror_title
|
title = self.keyerror_title
|
||||||
key_sequences = [key for keylist in self.currentKeySequences
|
key_sequences = [key for keylist in self.current_key_sequences
|
||||||
for key in keylist]
|
for key in keylist]
|
||||||
if not keys.endswith('>'):
|
if not keys.endswith('>'):
|
||||||
self.showerror(title, parent=self,
|
self.showerror(title, parent=self,
|
||||||
message='Missing the final Key')
|
message='Missing the final Key')
|
||||||
elif (not modifiers
|
elif (not modifiers
|
||||||
and finalKey not in self.functionKeys + self.moveKeys):
|
and final_key not in self.function_keys + self.move_keys):
|
||||||
self.showerror(title=title, parent=self,
|
self.showerror(title=title, parent=self,
|
||||||
message='No modifier key(s) specified.')
|
message='No modifier key(s) specified.')
|
||||||
elif (modifiers == ['Shift']) \
|
elif (modifiers == ['Shift']) \
|
||||||
and (finalKey not in
|
and (final_key not in
|
||||||
self.functionKeys + self.moveKeys + ('Tab', 'Space')):
|
self.function_keys + self.move_keys + ('Tab', 'Space')):
|
||||||
msg = 'The shift modifier by itself may not be used with'\
|
msg = 'The shift modifier by itself may not be used with'\
|
||||||
' this key symbol.'
|
' this key symbol.'
|
||||||
self.showerror(title=title, parent=self, message=msg)
|
self.showerror(title=title, parent=self, message=msg)
|
||||||
|
|
@ -273,12 +304,11 @@ class GetKeysDialog(Toplevel):
|
||||||
msg = 'This key combination is already in use.'
|
msg = 'This key combination is already in use.'
|
||||||
self.showerror(title=title, parent=self, message=msg)
|
self.showerror(title=title, parent=self, message=msg)
|
||||||
else:
|
else:
|
||||||
keysOK = True
|
return True
|
||||||
return keysOK
|
return False
|
||||||
|
|
||||||
def bind_ok(self, keys):
|
def bind_ok(self, keys):
|
||||||
"Return True if Tcl accepts the new keys else show message."
|
"Return True if Tcl accepts the new keys else show message."
|
||||||
|
|
||||||
try:
|
try:
|
||||||
binding = self.bind(keys, lambda: None)
|
binding = self.bind(keys, lambda: None)
|
||||||
except TclError as err:
|
except TclError as err:
|
||||||
|
|
|
||||||
|
|
@ -141,12 +141,11 @@ _editor_window_spec = {
|
||||||
"Best to close editor first."
|
"Best to close editor first."
|
||||||
}
|
}
|
||||||
|
|
||||||
# Update once issue21519 is resolved.
|
|
||||||
GetKeysDialog_spec = {
|
GetKeysDialog_spec = {
|
||||||
'file': 'config_key',
|
'file': 'config_key',
|
||||||
'kwds': {'title': 'Test keybindings',
|
'kwds': {'title': 'Test keybindings',
|
||||||
'action': 'find-again',
|
'action': 'find-again',
|
||||||
'currentKeySequences': [''] ,
|
'current_key_sequences': [['<Control-Key-g>', '<Key-F3>', '<Control-Key-G>']],
|
||||||
'_htest': True,
|
'_htest': True,
|
||||||
},
|
},
|
||||||
'msg': "Test for different key modifier sequences.\n"
|
'msg': "Test for different key modifier sequences.\n"
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
"Test config_key, coverage 75%"
|
"Test config_key, coverage 82%"
|
||||||
|
|
||||||
from idlelib import config_key
|
from idlelib import config_key
|
||||||
from test.support import requires
|
from test.support import requires
|
||||||
|
|
@ -9,15 +9,15 @@ from idlelib.idle_test.mock_tk import Mbox_func
|
||||||
|
|
||||||
|
|
||||||
class ValidationTest(unittest.TestCase):
|
class ValidationTest(unittest.TestCase):
|
||||||
"Test validation methods: OK, KeysOK, bind_ok."
|
"Test validation methods: ok, keys_ok, bind_ok."
|
||||||
|
|
||||||
class Validator(config_key.GetKeysDialog):
|
class Validator(config_key.GetKeysDialog):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
config_key.GetKeysDialog.__init__(self, *args, **kwargs)
|
config_key.GetKeysDialog.__init__(self, *args, **kwargs)
|
||||||
class listKeysFinal:
|
class list_keys_final:
|
||||||
get = Func()
|
get = Func()
|
||||||
self.listKeysFinal = listKeysFinal
|
self.list_keys_final = list_keys_final
|
||||||
GetModifiers = Func()
|
get_modifiers = Func()
|
||||||
showerror = Mbox_func()
|
showerror = Mbox_func()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
@ -31,7 +31,7 @@ class ValidationTest(unittest.TestCase):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def tearDownClass(cls):
|
def tearDownClass(cls):
|
||||||
cls.dialog.Cancel()
|
cls.dialog.cancel()
|
||||||
cls.root.update_idletasks()
|
cls.root.update_idletasks()
|
||||||
cls.root.destroy()
|
cls.root.destroy()
|
||||||
del cls.dialog, cls.root
|
del cls.dialog, cls.root
|
||||||
|
|
@ -42,49 +42,49 @@ class ValidationTest(unittest.TestCase):
|
||||||
# A test that sets a non-blank modifier list should reset it to [].
|
# A test that sets a non-blank modifier list should reset it to [].
|
||||||
|
|
||||||
def test_ok_empty(self):
|
def test_ok_empty(self):
|
||||||
self.dialog.keyString.set(' ')
|
self.dialog.key_string.set(' ')
|
||||||
self.dialog.OK()
|
self.dialog.ok()
|
||||||
self.assertEqual(self.dialog.result, '')
|
self.assertEqual(self.dialog.result, '')
|
||||||
self.assertEqual(self.dialog.showerror.message, 'No key specified.')
|
self.assertEqual(self.dialog.showerror.message, 'No key specified.')
|
||||||
|
|
||||||
def test_ok_good(self):
|
def test_ok_good(self):
|
||||||
self.dialog.keyString.set('<Key-F11>')
|
self.dialog.key_string.set('<Key-F11>')
|
||||||
self.dialog.listKeysFinal.get.result = 'F11'
|
self.dialog.list_keys_final.get.result = 'F11'
|
||||||
self.dialog.OK()
|
self.dialog.ok()
|
||||||
self.assertEqual(self.dialog.result, '<Key-F11>')
|
self.assertEqual(self.dialog.result, '<Key-F11>')
|
||||||
self.assertEqual(self.dialog.showerror.message, '')
|
self.assertEqual(self.dialog.showerror.message, '')
|
||||||
|
|
||||||
def test_keys_no_ending(self):
|
def test_keys_no_ending(self):
|
||||||
self.assertFalse(self.dialog.KeysOK('<Control-Shift'))
|
self.assertFalse(self.dialog.keys_ok('<Control-Shift'))
|
||||||
self.assertIn('Missing the final', self.dialog.showerror.message)
|
self.assertIn('Missing the final', self.dialog.showerror.message)
|
||||||
|
|
||||||
def test_keys_no_modifier_bad(self):
|
def test_keys_no_modifier_bad(self):
|
||||||
self.dialog.listKeysFinal.get.result = 'A'
|
self.dialog.list_keys_final.get.result = 'A'
|
||||||
self.assertFalse(self.dialog.KeysOK('<Key-A>'))
|
self.assertFalse(self.dialog.keys_ok('<Key-A>'))
|
||||||
self.assertIn('No modifier', self.dialog.showerror.message)
|
self.assertIn('No modifier', self.dialog.showerror.message)
|
||||||
|
|
||||||
def test_keys_no_modifier_ok(self):
|
def test_keys_no_modifier_ok(self):
|
||||||
self.dialog.listKeysFinal.get.result = 'F11'
|
self.dialog.list_keys_final.get.result = 'F11'
|
||||||
self.assertTrue(self.dialog.KeysOK('<Key-F11>'))
|
self.assertTrue(self.dialog.keys_ok('<Key-F11>'))
|
||||||
self.assertEqual(self.dialog.showerror.message, '')
|
self.assertEqual(self.dialog.showerror.message, '')
|
||||||
|
|
||||||
def test_keys_shift_bad(self):
|
def test_keys_shift_bad(self):
|
||||||
self.dialog.listKeysFinal.get.result = 'a'
|
self.dialog.list_keys_final.get.result = 'a'
|
||||||
self.dialog.GetModifiers.result = ['Shift']
|
self.dialog.get_modifiers.result = ['Shift']
|
||||||
self.assertFalse(self.dialog.KeysOK('<a>'))
|
self.assertFalse(self.dialog.keys_ok('<a>'))
|
||||||
self.assertIn('shift modifier', self.dialog.showerror.message)
|
self.assertIn('shift modifier', self.dialog.showerror.message)
|
||||||
self.dialog.GetModifiers.result = []
|
self.dialog.get_modifiers.result = []
|
||||||
|
|
||||||
def test_keys_dup(self):
|
def test_keys_dup(self):
|
||||||
for mods, final, seq in (([], 'F12', '<Key-F12>'),
|
for mods, final, seq in (([], 'F12', '<Key-F12>'),
|
||||||
(['Control'], 'x', '<Control-Key-x>'),
|
(['Control'], 'x', '<Control-Key-x>'),
|
||||||
(['Control'], 'X', '<Control-Key-X>')):
|
(['Control'], 'X', '<Control-Key-X>')):
|
||||||
with self.subTest(m=mods, f=final, s=seq):
|
with self.subTest(m=mods, f=final, s=seq):
|
||||||
self.dialog.listKeysFinal.get.result = final
|
self.dialog.list_keys_final.get.result = final
|
||||||
self.dialog.GetModifiers.result = mods
|
self.dialog.get_modifiers.result = mods
|
||||||
self.assertFalse(self.dialog.KeysOK(seq))
|
self.assertFalse(self.dialog.keys_ok(seq))
|
||||||
self.assertIn('already in use', self.dialog.showerror.message)
|
self.assertIn('already in use', self.dialog.showerror.message)
|
||||||
self.dialog.GetModifiers.result = []
|
self.dialog.get_modifiers.result = []
|
||||||
|
|
||||||
def test_bind_ok(self):
|
def test_bind_ok(self):
|
||||||
self.assertTrue(self.dialog.bind_ok('<Control-Shift-Key-a>'))
|
self.assertTrue(self.dialog.bind_ok('<Control-Shift-Key-a>'))
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
Apply PEP8 naming convention to config_key.py.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue