Python 绑定到tkinter中每个键的Keybind
我正在用Python创建一个交互式游戏,我正在尝试用“按任意键继续”的说明进行介绍。我在将所有键绑定到单个动作时遇到了一些困难 我已尝试绑定到Python 绑定到tkinter中每个键的Keybind,python,python-3.x,tkinter,key-bindings,Python,Python 3.x,Tkinter,Key Bindings,我正在用Python创建一个交互式游戏,我正在尝试用“按任意键继续”的说明进行介绍。我在将所有键绑定到单个动作时遇到了一些困难 我已尝试绑定到”,但它会显示一条错误消息 from tkinter import * window = Tk() root = Canvas(window, width=500, height=500) def testing(): print("Hello World!") root.bind_all('<Any>',
”
,但它会显示一条错误消息
from tkinter import *
window = Tk()
root = Canvas(window, width=500, height=500)
def testing():
print("Hello World!")
root.bind_all('<Any>', testing)
root.pack()
root.mainloop()
从tkinter导入*
window=Tk()
根=画布(窗口,宽度=500,高度=500)
def测试():
打印(“你好,世界!”)
root.bind_all(“”,测试)
root.pack()
root.mainloop()
如前所述,'
keybind会导致一条错误消息,内容为:tkinter.TclError:bad event type或keysym“Any”
。是否有一种简单的方法将每个键绑定到一个动作?我使用
它将捕获任何键盘事件并打印“Hello”。不要忘记在testing()
中指定event
或event=None
参数
from tkinter import *
window = Tk()
root = Canvas(window, width=500, height=500)
def testing(event):
print("Hello!")
def countdown(count, label):
label['text'] = count
if count > -1:
root.after(1000, countdown, count-1, label)
elif count == 0:
label['text'] = 'Time Expired'
elif count < 0:
label.destroy()
root.bind_all('<Key>', testing)
root.pack()
root.mainloop()
从tkinter导入*
window=Tk()
根=画布(窗口,宽度=500,高度=500)
def测试(事件):
打印(“你好!”)
def倒计时(计数、标签):
标签['text']=计数
如果计数>-1:
root.after(1000,倒计时,计数-1,标签)
elif计数==0:
标签['text']='Time Expired'
elif计数<0:
label.destroy()
root.bind_all(“”,测试)
root.pack()
root.mainloop()