python - 如何在完成其工作后删除按键绑定(bind)

标签 python python-3.x tkinter key-bindings

我创建了一个按键绑定(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/

相关文章:

python - 尝试从 lambda(python)访问 S3 时出错

python - 将元组的整数转换为元组的字符串

python - 如何用印地语将数据写入文件?

python - 如何在不使用 .place 的情况下临时删除(不是删除和替换)Tkinter 小部件?

python - 从Python中的配置文件读取 token

python - 现代 glTranslate 和 glRotate 的替代品是什么?

python - 如何修复错误 "_core.QgsProcessingException: There were errors executing the algorithm."

python-3.x - 如何将自定义数据加载到 keras cyclegan 示例的 tfds 中?

python - 将非连续值分组到列表中

python - Tkinter KeyPress 和 KeyRelease 事件