简体   繁体   中英

Button Disable on press tkinter

I'm making a mining game and whenever a user clicks 'mine' button, I want it to disable so the user cant click it again until cool down wears off. I made a sample of the code, I state the definition first, then make the button, but since the button is after, the def doesn't know what variable the 'mine' button is. Any help appreciated!



root = Tk()


def def1():
    btn[state] = 'disabled'

Btn = Button(root, text="button", command= def1())



root.mainloop()```

Try this:

import tkinter as tk


def enable_btn():
    btn.config(state="normal")

def def1():
    print("Clicked")
    btn.config(state="disabled")
    # 1000 is the cooldown in ms (so 1000 = 1 sec)
    btn.after(1000, enable_btn)

root = tk.Tk()

btn = tk.Button(root, text="button", command=def1)
btn.pack()

root.mainloop()

I am using a .after script so the enable_btn function runs 1 sec after def1 is called.

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