如何在完成其工作后删除键绑定

问题描述 投票:1回答:1

我创建了一个keybind,并希望在激活后删除它。我该怎么做呢?

我在我的代码中尝试了这个:

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()

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

python python-3.x tkinter key-bindings
1个回答
3
投票

试试这个

from tkinter import *
root = Tk()


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


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

对于unbind,所有小部件都使用函数.unbind_all('<Key>')

© www.soinside.com 2019 - 2024. All rights reserved.