0

My Problem today is, that I don't get Canvas hotkeys bindings working.. Working example with Mousebutton:

def abc(event):
    print("abc")
root = Tk()
canv = Canvas(root)
canv.pack(expand=True, fill=BOTH)
canv.bind("<Button-1>", abc)
root.mainloop()

Not working with "a" letter:

def abc(event):
    print("abc")
root = Tk()
canv = Canvas(root)
canv.pack(expand=True, fill=BOTH)
canv.bind("a", abc)
root.mainloop()

How do I get this working?

1 Answers1

0

If I recall correctly, the Canvas widget doesn't receive focus when you click on it, so key press events don't get executed. Consider binding your keyboard shortcuts to the root instead.

def abc(event):
    print("abc")
root = Tk()
canv = Canvas(root)
canv.pack(expand=True, fill=BOTH)
root.bind("a", abc)
root.mainloop()
Kevin
  • 74,910
  • 12
  • 133
  • 166