我创建了一个按键绑定(bind),并希望在激活后将其删除。我该怎么做?
我在我的代码中尝试过这个:
def testing(event):
print("Hello!")
root.bind_all('<Key>', testing)
root.deletecommand('<Key>', testing)
但是,这不起作用,因为 Python 显示一条错误消息,指出 deletecommand() takes 2 positional arguments but 3 were given
,当我只给出两个参数时。我也尝试过root.delete('<Key>', testing)
,但这也失败了。
from tkinter import *
def testing(event):
print("Hello!")
root.bind_all('<Key>', testing)
root.deletecommand('<Key>', testing)
root.pack()
root.mainloop()
我希望程序在完成其工作后能够删除按键绑定(bind)。然而,正如前面提到的,Python 显示了一条错误消息。如何解决这个问题?
最佳答案
尝试这样
from tkinter import *
root = Tk()
def testing(event):
print("Hello!")
root.unbind_all('<Key>')
root.bind_all('<Key>', testing)
root.mainloop()
要取消绑定(bind)所有小部件,请使用函数 .unbind_all('<Key>')
.
关于python - 如何在完成其工作后删除按键绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55641760/