I created a keybind, and want to delete it after it is activated. How do I do this?
I have tried this in my code:
JavaScript
x
6
1
def testing(event):
2
print("Hello!")
3
4
root.bind_all('<Key>', testing)
5
root.deletecommand('<Key>', testing)
6
However, this does not work, as Python displays an error message stating that deletecommand() takes 2 positional arguments but 3 were given
, when I only gave two arguments. I also tried root.delete('<Key>', testing)
, but this also fails.
JavaScript
1
11
11
1
from tkinter import *
2
3
def testing(event):
4
print("Hello!")
5
6
root.bind_all('<Key>', testing)
7
root.deletecommand('<Key>', testing)
8
9
root.pack()
10
root.mainloop()
11
I was hoping that the program would remove the keybind after it did its job. However, Python displayed an error message, as mentioned before. How do I fix this problem?
Advertisement
Answer
try as this
JavaScript
1
12
12
1
from tkinter import *
2
root = Tk()
3
4
5
def testing(event):
6
print("Hello!")
7
root.unbind_all('<Key>')
8
9
10
root.bind_all('<Key>', testing)
11
root.mainloop()
12
For unbind all the widget use the function .unbind_all('<Key>')
.