<s> """<STR_LIT>""" <EOL> from Tkinter import * <EOL> import tkMessageBox <EOL> import string <EOL> import sys <EOL> class GetKeysDialog ( Toplevel ) : <EOL> def __init__ ( self , parent , title , action , currentKeySequences ) : <EOL> """<STR_LIT>""" <EOL> Toplevel . __init__ ( self , parent ) <EOL> self . configure ( borderwidth = <NUM_LIT:5> ) <EOL> self . resizable ( height = FALSE , width = FALSE ) <EOL> self . title ( title ) <EOL> self . transient ( parent ) <EOL> self . grab_set ( ) <EOL> self . protocol ( "<STR_LIT>" , self . Cancel ) <EOL> self . parent = parent <EOL> self . action = action <EOL> self . currentKeySequences = currentKeySequences <EOL> self . result = '<STR_LIT>' <EOL> self . keyString = StringVar ( self ) <EOL> self . keyString . set ( '<STR_LIT>' ) <EOL> self . SetModifiersForPlatform ( ) <EOL> self . modifier_vars = [ ] <EOL> for modifier in self . modifiers : <EOL> variable = StringVar ( self ) <EOL> variable . set ( '<STR_LIT>' ) <EOL> self . modifier_vars . append ( variable ) <EOL> self . advanced = False <EOL> self . CreateWidgets ( ) <EOL> self . LoadFinalKeyList ( ) <EOL> self . withdraw ( ) <EOL> self . update_idletasks ( ) <EOL> self . geometry ( "<STR_LIT>" % <EOL> ( ( parent . winfo_rootx ( ) + ( ( parent . winfo_width ( ) / <NUM_LIT:2> ) <EOL> - ( self . winfo_reqwidth ( ) / <NUM_LIT:2> ) ) , <EOL> parent . winfo_rooty ( ) + ( ( parent . winfo_height ( ) / <NUM_LIT:2> ) <EOL> - ( self . winfo_reqheight ( ) / <NUM_LIT:2> ) ) ) ) ) <EOL> self . deiconify ( ) <EOL> self . wait_window ( ) <EOL> def CreateWidgets ( self ) : <EOL> frameMain = Frame ( self , borderwidth = <NUM_LIT:2> , relief = SUNKEN ) <EOL> frameMain . pack ( side = TOP , expand = TRUE , fill = BOTH ) <EOL> frameButtons = Frame ( self ) <EOL> frameButtons . pack ( side = BOTTOM , fill = X ) <EOL> self . buttonOK = Button ( frameButtons , text = '<STR_LIT:OK>' , <EOL> width = <NUM_LIT:8> , command = self . OK ) <EOL> self . buttonOK . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:0> , padx = <NUM_LIT:5> , pady = <NUM_LIT:5> ) <EOL> self . buttonCancel = Button ( frameButtons , text = '<STR_LIT>' , <EOL> width = <NUM_LIT:8> , command = self . Cancel ) <EOL> self . buttonCancel . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:1> , padx = <NUM_LIT:5> , pady = <NUM_LIT:5> ) <EOL> self . frameKeySeqBasic = Frame ( frameMain ) <EOL> self . frameKeySeqAdvanced = Frame ( frameMain ) <EOL> self . frameControlsBasic = Frame ( frameMain ) <EOL> self . frameHelpAdvanced = Frame ( frameMain ) <EOL> self . frameKeySeqAdvanced . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:0> , sticky = NSEW , padx = <NUM_LIT:5> , pady = <NUM_LIT:5> ) <EOL> self . frameKeySeqBasic . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:0> , sticky = NSEW , padx = <NUM_LIT:5> , pady = <NUM_LIT:5> ) <EOL> self . frameKeySeqBasic . lift ( ) <EOL> self . frameHelpAdvanced . grid ( row = <NUM_LIT:1> , column = <NUM_LIT:0> , sticky = NSEW , padx = <NUM_LIT:5> ) <EOL> self . frameControlsBasic . grid ( row = <NUM_LIT:1> , column = <NUM_LIT:0> , sticky = NSEW , padx = <NUM_LIT:5> ) <EOL> self . frameControlsBasic . lift ( ) <EOL> self . buttonLevel = Button ( frameMain , command = self . ToggleLevel , <EOL> text = '<STR_LIT>' ) <EOL> self . buttonLevel . grid ( row = <NUM_LIT:2> , column = <NUM_LIT:0> , stick = EW , padx = <NUM_LIT:5> , pady = <NUM_LIT:5> ) <EOL> labelTitleBasic = Label ( self . frameKeySeqBasic , <EOL> text = "<STR_LIT>" + self . action + "<STR_LIT>" ) <EOL> labelTitleBasic . pack ( anchor = W ) <EOL> labelKeysBasic = Label ( self . frameKeySeqBasic , justify = LEFT , <EOL> textvariable = self . keyString , relief = GROOVE , borderwidth = <NUM_LIT:2> ) <EOL> labelKeysBasic . pack ( ipadx = <NUM_LIT:5> , ipady = <NUM_LIT:5> , fill = X ) <EOL> self . modifier_checkbuttons = { } <EOL> column = <NUM_LIT:0> <EOL> for modifier , variable in zip ( self . modifiers , self . modifier_vars ) : <EOL> label = self . modifier_label . get ( modifier , modifier ) <EOL> check = Checkbutton ( self . frameControlsBasic , <EOL> command = self . BuildKeyString , <EOL> text = label , variable = variable , onvalue = modifier , offvalue = '<STR_LIT>' ) <EOL> check . grid ( row = <NUM_LIT:0> , column = column , padx = <NUM_LIT:2> , sticky = W ) <EOL> self . modifier_checkbuttons [ modifier ] = check <EOL> column += <NUM_LIT:1> <EOL> labelFnAdvice = Label ( self . frameControlsBasic , justify = LEFT , <EOL> text = "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> labelFnAdvice . grid ( row = <NUM_LIT:1> , column = <NUM_LIT:0> , columnspan = <NUM_LIT:4> , padx = <NUM_LIT:2> , sticky = W ) <EOL> self . listKeysFinal = Listbox ( self . frameControlsBasic , width = <NUM_LIT:15> , height = <NUM_LIT:10> , <EOL> selectmode = SINGLE ) <EOL> self . listKeysFinal . bind ( '<STR_LIT>' , self . FinalKeySelected ) <EOL> self . listKeysFinal . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:4> , rowspan = <NUM_LIT:4> , sticky = NS ) <EOL> scrollKeysFinal = Scrollbar ( self . frameControlsBasic , orient = VERTICAL , <EOL> command = self . listKeysFinal . yview ) <EOL> self . listKeysFinal . config ( yscrollcommand = scrollKeysFinal . set ) <EOL> scrollKeysFinal . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:5> , rowspan = <NUM_LIT:4> , sticky = NS ) <EOL> self . buttonClear = Button ( self . frameControlsBasic , <EOL> text = '<STR_LIT>' , command = self . ClearKeySeq ) <EOL> self . buttonClear . grid ( row = <NUM_LIT:2> , column = <NUM_LIT:0> , columnspan = <NUM_LIT:4> ) <EOL> labelTitleAdvanced = Label ( self . frameKeySeqAdvanced , justify = LEFT , <EOL> text = "<STR_LIT>" + self . action + "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> labelTitleAdvanced . pack ( anchor = W ) <EOL> self . entryKeysAdvanced = Entry ( self . frameKeySeqAdvanced , <EOL> textvariable = self . keyString ) <EOL> self . entryKeysAdvanced . pack ( fill = X ) <EOL> labelHelpAdvanced = Label ( self . frameHelpAdvanced , justify = LEFT , <EOL> text = "<STR_LIT>" + <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> labelHelpAdvanced . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:0> , sticky = NSEW ) <EOL> def SetModifiersForPlatform ( self ) : <EOL> """<STR_LIT>""" <EOL> if sys . platform == "<STR_LIT>" : <EOL> self . modifiers = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> else : <EOL> self . modifiers = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . modifier_label = { '<STR_LIT>' : '<STR_LIT>' } <EOL> def ToggleLevel ( self ) : <EOL> if self . buttonLevel . cget ( '<STR_LIT:text>' ) [ : <NUM_LIT:8> ] == '<STR_LIT>' : <EOL> self . ClearKeySeq ( ) <EOL> self . buttonLevel . config ( text = '<STR_LIT>' ) <EOL> self . frameKeySeqAdvanced . lift ( ) <EOL> self . frameHelpAdvanced . lift ( ) <EOL> self . entryKeysAdvanced . focus_set ( ) <EOL> self . advanced = True <EOL> else : <EOL> self . ClearKeySeq ( ) <EOL> self . buttonLevel . config ( text = '<STR_LIT>' ) <EOL> self . frameKeySeqBasic . lift ( ) <EOL> self . frameControlsBasic . lift ( ) <EOL> self . advanced = False <EOL> def FinalKeySelected ( self , event ) : <EOL> self . BuildKeyString ( ) <EOL> def BuildKeyString ( self ) : <EOL> keyList = modifiers = self . GetModifiers ( ) <EOL> finalKey = self . listKeysFinal . get ( ANCHOR ) <EOL> if finalKey : <EOL> finalKey = self . TranslateKey ( finalKey , modifiers ) <EOL> keyList . append ( finalKey ) <EOL> self . keyString . set ( '<STR_LIT:<>' + string . join ( keyList , '<STR_LIT:->' ) + '<STR_LIT:>>' ) <EOL> def GetModifiers ( self ) : <EOL> modList = [ variable . get ( ) for variable in self . modifier_vars ] <EOL> return [ mod for mod in modList if mod ] <EOL> def ClearKeySeq ( self ) : <EOL> self . listKeysFinal . select_clear ( <NUM_LIT:0> , END ) <EOL> self . listKeysFinal . yview ( MOVETO , '<STR_LIT>' ) <EOL> for variable in self . modifier_vars : <EOL> variable . set ( '<STR_LIT>' ) <EOL> self . keyString . set ( '<STR_LIT>' ) <EOL> def LoadFinalKeyList ( self ) : <EOL> self . functionKeys = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . alphanumKeys = tuple ( string . ascii_lowercase + string . digits ) <EOL> self . punctuationKeys = tuple ( '<STR_LIT>' ) <EOL> self . whitespaceKeys = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . editKeys = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . moveKeys = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> keys = ( self . alphanumKeys + self . punctuationKeys + self . functionKeys + <EOL> self . whitespaceKeys + self . editKeys + self . moveKeys ) <EOL> self . listKeysFinal . insert ( END , * keys ) <EOL> def TranslateKey ( self , key , modifiers ) : <EOL> "<STR_LIT>" <EOL> translateDict = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:!>' : '<STR_LIT>' , '<STR_LIT:@>' : '<STR_LIT>' , '<STR_LIT:#>' : '<STR_LIT>' , <EOL> '<STR_LIT:%>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:&>' : '<STR_LIT>' , '<STR_LIT:*>' : '<STR_LIT>' , <EOL> '<STR_LIT:(>' : '<STR_LIT>' , '<STR_LIT:)>' : '<STR_LIT>' , '<STR_LIT:_>' : '<STR_LIT>' , '<STR_LIT:->' : '<STR_LIT>' , <EOL> '<STR_LIT:+>' : '<STR_LIT>' , '<STR_LIT:=>' : '<STR_LIT>' , '<STR_LIT:{>' : '<STR_LIT>' , '<STR_LIT:}>' : '<STR_LIT>' , <EOL> '<STR_LIT:[>' : '<STR_LIT>' , '<STR_LIT:]>' : '<STR_LIT>' , '<STR_LIT:|>' : '<STR_LIT:bar>' , '<STR_LIT:;>' : '<STR_LIT>' , <EOL> '<STR_LIT::>' : '<STR_LIT>' , '<STR_LIT:U+002C>' : '<STR_LIT>' , '<STR_LIT:.>' : '<STR_LIT>' , '<STR_LIT:<>' : '<STR_LIT>' , '<STR_LIT:>>' : '<STR_LIT>' , <EOL> '<STR_LIT:/>' : '<STR_LIT>' , '<STR_LIT:?>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> if key in translateDict . keys ( ) : <EOL> key = translateDict [ key ] <EOL> if '<STR_LIT>' in modifiers and key in string . ascii_lowercase : <EOL> key = key . upper ( ) <EOL> key = '<STR_LIT>' + key <EOL> return key <EOL> def OK ( self , event = None ) : <EOL> if self . advanced or self . KeysOK ( ) : <EOL> self . result = self . keyString . get ( ) <EOL> self . destroy ( ) <EOL> def Cancel ( self , event = None ) : <EOL> self . result = '<STR_LIT>' <EOL> self . destroy ( ) <EOL> def KeysOK ( self ) : <EOL> '''<STR_LIT>''' <EOL> keys = self . keyString . get ( ) <EOL> keys . strip ( ) <EOL> finalKey = self . listKeysFinal . get ( ANCHOR ) <EOL> modifiers = self . GetModifiers ( ) <EOL> keySequence = keys . split ( ) <EOL> keysOK = False <EOL> title = '<STR_LIT>' <EOL> if not keys : <EOL> tkMessageBox . showerror ( title = title , parent = self , <EOL> message = '<STR_LIT>' ) <EOL> elif not keys . endswith ( '<STR_LIT:>>' ) : <EOL> tkMessageBox . showerror ( title = title , parent = self , <EOL> message = '<STR_LIT>' ) <EOL> elif ( not modifiers <EOL> and finalKey not in self . functionKeys + self . moveKeys ) : <EOL> tkMessageBox . showerror ( title = title , parent = self , <EOL> message = '<STR_LIT>' ) <EOL> elif ( modifiers == [ '<STR_LIT>' ] ) and ( finalKey not in <EOL> self . functionKeys + self . moveKeys + ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> msg = '<STR_LIT>' '<STR_LIT>' <EOL> tkMessageBox . showerror ( title = title , parent = self , message = msg ) <EOL> elif keySequence in self . currentKeySequences : <EOL> msg = '<STR_LIT>' <EOL> tkMessageBox . showerror ( title = title , parent = self , message = msg ) <EOL> else : <EOL> keysOK = True <EOL> return keysOK <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> root = Tk ( ) <EOL> def run ( ) : <EOL> keySeq = '<STR_LIT>' <EOL> dlg = GetKeysDialog ( root , '<STR_LIT>' , '<STR_LIT>' , [ ] ) <EOL> print dlg . result <EOL> Button ( root , text = '<STR_LIT>' , command = run ) . pack ( ) <EOL> root . mainloop ( ) </s>
