Bob Pynewby
Bob Pynewby

Reputation: 29

python bind code not working as expected

Why does the following code not produce the expected result?:

from Tkinter import *
root = Tk()
def key(event):
    frame.focus_set()
    print "pressed", repr(event.char
frame = Frame(root, width=100, height=100)
frame.bind("<Key>", key)
frame.pack()
root.mainloop()

Upvotes: 1

Views: 129

Answers (1)

werewindle
werewindle

Reputation: 3029

You need to set input focus to your frame. Try adding frame.focus_set() before

root.mainloop()

Upvotes: 1

Related Questions