python unbinding/disable key binding after click and resume it later

key

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 I tried so far:

self.choiceA = self.master.bind('a', self.run1) #bind key "a" to run1
def run1(self, event=None):
    self.draw_confirmation_button1()
    self.master.unbind('a', self.choiceA) #try1: use "unbind", doesn't work

    self.choiceA.configure(state='disabled') #try2: use state='disabled', doesn't't work, I assume it only works for button
    self.master.after(2000, lambda:self.choiceA.configure(state="normal"))

Further, how can I re-enable the key after 2s?

Thank you so much!

j_4321

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

def callback(event):
    print("Disable binding for 2s")
    root.unbind("<a>", bind_id)
    root.after(2000, rebind)  # wait for 2000 ms and rebind key a

def rebind():
    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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Resume PullSubscription later for EWS

From Dev

Knockout click binding after bubbling

From Dev

Binding breaks after button click

From Dev

How resume app after click notification with Java Android?

From Dev

Binding Click event after append Jquery not working

From Dev

Key binding to reload .emacs after changing it?

From Dev

ng-click stops working after first click, but only in later angular versions

From Dev

Pause a running script in Mac terminal and then resume later

From Dev

How to Stop/Wait a Thread and then Resume/Restart it later

From Dev

Inside Activity, How to pause a for loop to call a fragment and after button click on fragment then resume the loop to start again

From Dev

Python Tkinter - recovering original default key binding

From Dev

Angular JS: binding a click event after angular renders HTML DOM

From Dev

Windows phone 8 slider binding works only after a click

From Dev

jQuery: Not able to uncheck checkbox after binding click event

From Dev

Windows phone 8 slider binding works only after a click

From Dev

Unable to resume after suspend

From Dev

Resume a function after clearInterval

From Dev

Resume timer after pausing it?

From Dev

Cannot resume after hibernate

From Dev

Unable to resume after suspend

From Dev

Cannot resume after hibernate

From Dev

Resume AVPlayer after forwardPlaybackEndTime

From Dev

Restart Touchscreen after resume

From Dev

Resume wget after canceled

From Dev

How to resume execution after pressing pause/break key in windows command line

From Dev

HTML JS/JQuery: Store Parameter after button click to use it for a later function

From Dev

Binding a key to be the Menu Key

From Dev

How can I preserve and later resume a thread's execution state?

From Dev

Is there a way to pause a running process on Linux systems and resume later?

Related Related

  1. 1

    Resume PullSubscription later for EWS

  2. 2

    Knockout click binding after bubbling

  3. 3

    Binding breaks after button click

  4. 4

    How resume app after click notification with Java Android?

  5. 5

    Binding Click event after append Jquery not working

  6. 6

    Key binding to reload .emacs after changing it?

  7. 7

    ng-click stops working after first click, but only in later angular versions

  8. 8

    Pause a running script in Mac terminal and then resume later

  9. 9

    How to Stop/Wait a Thread and then Resume/Restart it later

  10. 10

    Inside Activity, How to pause a for loop to call a fragment and after button click on fragment then resume the loop to start again

  11. 11

    Python Tkinter - recovering original default key binding

  12. 12

    Angular JS: binding a click event after angular renders HTML DOM

  13. 13

    Windows phone 8 slider binding works only after a click

  14. 14

    jQuery: Not able to uncheck checkbox after binding click event

  15. 15

    Windows phone 8 slider binding works only after a click

  16. 16

    Unable to resume after suspend

  17. 17

    Resume a function after clearInterval

  18. 18

    Resume timer after pausing it?

  19. 19

    Cannot resume after hibernate

  20. 20

    Unable to resume after suspend

  21. 21

    Cannot resume after hibernate

  22. 22

    Resume AVPlayer after forwardPlaybackEndTime

  23. 23

    Restart Touchscreen after resume

  24. 24

    Resume wget after canceled

  25. 25

    How to resume execution after pressing pause/break key in windows command line

  26. 26

    HTML JS/JQuery: Store Parameter after button click to use it for a later function

  27. 27

    Binding a key to be the Menu Key

  28. 28

    How can I preserve and later resume a thread's execution state?

  29. 29

    Is there a way to pause a running process on Linux systems and resume later?

HotTag

Archive