简体   繁体   中英

Preventing a Button Press From Registering TKInter/Python 3.3

I'm in the process of creating a filling program with Python 3.3/Tkinter and have run into an issue that's really stumping me. Basically, I want to prevent button presses from registering while certain processes are taking place. I tried changing the button's state to state='disabled' but this only delays the click from registering until after the function is finished running. In other words, despite the button being "disabled", if it's clicked, the button press will register as soon as it's re-enabled. Please see the example code below.

def Button_Action:
    Button.config(state='disabled')
    #Activate some hardware that takes a few seconds.
    Button.config(state='normal')

So, the question is: How can one selectively ignore button presses in Tkinter/Python 3?

I'm really new to Python and have tried searching for relevant questions to no avail, so please forgive me if this is a stupid question, or has been asked before. Also, I have tested this with both Radiobutton s as well as, standard Button s (in case that helps).

You can use update method.

def button_action():
    b.config(state='disabled')
    b.update() # <----------
    # Activate some hardware that takes a few seconds.
    b.update() # <----------
    b.config(state='normal')

The first call to update is to make the button displayed as disabled state.

The second call to update is to handle all pending event (clicks in your case) before enable the button.

BTW, it's normal state, not enabled that make the button back to normal state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM