1.11M Members

Tkinter Keypress Event (Python)


If you press any key on your keyboard, this small Tkinter GUI program will tell you which key and what type of key has been pressed. Great for applications where a simple key stroke is required.

# bind and show a key press event with Tkinter
# tested with Python24      vegaseat     20nov2006

from Tkinter import *

root = Tk()
prompt = '      Press any key      '
label1 = Label(root, text=prompt, width=len(prompt), bg='yellow')

def key(event):
    if event.char == event.keysym:
        msg = 'Normal Key %r' % event.char
    elif len(event.char) == 1:
        msg = 'Punctuation Key %r (%r)' % (event.keysym, event.char)
        msg = 'Special Key %r' % event.keysym

root.bind_all('<Key>', key)

Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

This is an OP Kudos discussion and contributors may be rewarded
Start New Discussion
Tags Related to this Article