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