Get key code : Key Event « Tkinker « Python Tutorial






Get key code
from Tkinter import *
     
def showPosEvent(event):
    print 'Widget=%s X=%s Y=%s' % (event.widget, event.x, event.y)
     
def onKeyPress(event):
    print 'Got key press:', event.char
     
tkroot = Tk()
labelfont = ('courier', 20, 'bold')                
widget = Label(tkroot, text='Hello bind world')
widget.config(bg='red', font=labelfont)            
widget.config(height=5, width=20)                  
widget.pack(expand=YES, fill=BOTH)

widget.bind('<KeyPress>',  onKeyPress)             
widget.focus()                                     
tkroot.title('Click Me')
tkroot.mainloop()








18.16.Key Event
18.16.1.Get key codeGet key code
18.16.2.Up arrow keyUp arrow key
18.16.3.Enter key eventEnter key event
18.16.4.Bind key action to buttonBind key action to button
18.16.5.Entry components and event binding demonstration.Entry components and event binding demonstration.
18.16.6.Binding keys to keyboard events.Binding keys to keyboard events.