完成工作后如何删除键箱

时间:2019-04-11 21:59:16

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

我创建了一个绑定,并希望在激活它后将其删除。我该怎么做?

我已经在我的代码中尝试过此操作:

def testing(event):
    print("Hello!")

root.bind_all('<Key>', testing)
root.deletecommand('<Key>', testing)

但是,这不起作用,因为当我仅给出两个参数时,Python会显示一条错误消息,指出deletecommand() takes 2 positional arguments but 3 were given

from tkinter import *

def testing(event):
    print("Hello!")

root.bind_all('<Key>', testing)
root.deletecommand('<Key>', testing)

root.pack()
root.mainloop()

我希望程序在完成其工作后将其删除。但是,Python显示了一条错误消息,如前所述。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

尝试这个

from tkinter import *
root = Tk()


def testing(event):
    print("Hello!")
    root.unbind_all('<Key>')


root.bind_all('<Key>', testing)
root.mainloop()

要取消绑定所有小部件,请使用功能.unbind_all('<Key>')