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

2024/10/14 7:17:27

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 workself.choiceA.configure(state='disabled') #try2: use state='disabled', doesn't't work, I assume it only works for buttonself.master.after(2000, lambda:self.choiceA.configure(state="normal"))

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

Thank you so much!

Answer

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 tkdef callback(event):print("Disable binding for 2s")root.unbind("<a>", bind_id)root.after(2000, rebind)  # wait for 2000 ms and rebind key adef rebind():global bind_idbind_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).

https://en.xdnf.cn/q/117985.html

Related Q&A

Extracting information from pandas dataframe

I have the below dataframe. I want to build a rule engine to extract the tokens where the pattern is like Eg. "UNITED STATES" .What is the best way to do it ? Is there anything like regex o…

scipy import error with pyinstaller

I am trying to build a "One File" executable for my project with pyinstaller and a .spec file. The content of the spec file is as follows:# -*- mode: python -*-block_cipher = Nonea = Analysi…

How to compare meaningful level of a set of phrase that describe same concept in NLP?

I have two terms "vehicle" and "motor vehicle". Are there any way to compare the meaningfulness level or ambiguity level of these two in NLP? The outcome should be that "motor…

TypeError: slice indices must be integers or None or have an __index__ method. How to resolve it?

if w<h:normalized_char = np.ones((h, h), dtype=uint8)start = (h-w)/2normalized_char[:, start:start+w] = charelse:normalized_char = np.ones((w, w), dtype=uint8)start = (w-h)/2normalized_char[start:st…

Keras: Understanding the number of trainable LSTM parameters

I have run a Keras LSTM demo containing the following code (after line 166):m = 1 model=Sequential() dim_in = m dim_out = m nb_units = 10model.add(LSTM(input_shape=(None, dim_in),return_sequences=True,…

Updating Labels in Tkinter with for loop

So Im trying to print items in a list dynamically on 10 tkinter Labels using a for loop. Currently I have the following code:labe11 = StringVar() list2_placer = 0 list1_placer = 1 mover = 227 for items…

Paginate results, offset and limit

If I am developing a web service for retrieving some album names of certain artist using an API, and I am asked:The service should give the possibility to paginate results. It should support ofset= and…

Improve code to find prime numbers

I wrote this python code about 3 days ago, and I am stuck here, I think it could be better, but I dont know how to improve it. Can you guys please help me?# Function def is_prime(n):if n == 2 or n == …

How to read the line that contains a string then extract this line without this string

I have a file .txt that contains a specific line, like thisfile.txt. . T - Python and Matplotlib Essentials for Scientists and Engineers . A - Wood, M.A. . . .I would like to extract lines that contain…

Python: How to access and iterate over a list of div class element using (BeautifulSoup)

Im parsing data about car production with BeautifulSoup (see also my first question):from bs4 import BeautifulSoup import stringhtml = """ <h4>Production Capacity (year)</h4>…