Skip to content Skip to sidebar Skip to footer

Python Unbinding/disable Key Binding After Click And Resume It Later

I'm trying to unbind/disable key once it's clicked, and resume its function after 2s. But I can't figure out the code for the unbinding. The bind is on window. Here's the code that

Solution 1:

self.master.unbind('a', self.choiceA) does not work because the second argument you gave is the callback you want to unbind instead of the id returned when the binding was made.

In order to delay the re-binding, you need to use the .after(delay, callback) method where delay is in ms and callback is a function that does not take any argument.

import tkinter as tk

defcallback(event):
    print("Disable binding for 2s")
    root.unbind("<a>", bind_id)
    root.after(2000, rebind)  # wait for 2000 ms and rebind key adefrebind():
    global bind_id
    bind_id = root.bind("<a>", callback)
    print("Bindind on")


root = tk.Tk()
# store the binding id to be able to unbind it
bind_id = root.bind("<a>", callback)

root.mainloop()

Remark: since you use a class, my bind_id global variable will be an attribute for you (self.bind_id).

Post a Comment for "Python Unbinding/disable Key Binding After Click And Resume It Later"