我一直在为“4 in a row”游戏编写代码,到目前为止我已经解决了很多事情。
我用谷歌搜索了它,但我确实找不到任何可以将键放在绑定(bind)键字段中的选项。
board_canvas.bind("<Key>", some_random_callback_hihi)
更清楚地说,我的问题是我是否可以用任何其他特定键替换“”。例如,如何使用键 "<2>"
进行绑定(bind)?
最佳答案
根据this页面,
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.
...因此要绑定(bind)“A”键,您需要这样做
board_canvas.bind("a", some_random_callback_hihi)
要绑定(bind)“2”键,您可以这样做
board_canvas.bind("2", some_random_callback_hihi)
等等
注意:默认情况下, Canvas 不会获得键盘焦点。您需要安排它具有焦点。欲了解更多信息,请参阅Python Tkinter Canvas fail to bind keyboard .
关于python - 绑定(bind)键字段中可以包含哪些键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29991298/