1

I've been working on a code for the "4 in a row" game, and I've a lot of things settled so far.

I googled it but I really couldn't find any options for which keys can be in the binding key field.

board_canvas.bind("<Key>", some_random_callback_hihi)

My question, to be more clear is if I can replace "" with any kind of other specific key. For example, how would you bind with the key "<2>"?

nbro
  • 15,395
  • 32
  • 113
  • 196
Crimson Rin
  • 145
  • 1
  • 1
  • 3

1 Answers1

2

According to this page,

There are also various ways to simplify the event string; for example, to match a keyboard key, you can leave out the angle brackets and just use the key as is.

... so to bind the "A" key, you would do

board_canvas.bind("a", some_random_callback_hihi)

And to bind the "2" key, you would do

board_canvas.bind("2", some_random_callback_hihi)

Etc.

Note: the canvas doesn't get keyboard focus by default. You will need to arrange for it to have focus. For more information see Python Tkinter Canvas fail to bind keyboard .

Community
  • 1
  • 1
Kevin
  • 74,910
  • 12
  • 133
  • 166