[英]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.
我做了一个代码示例,我 state 先定义,然后制作按钮,但是由于按钮在后面,所以定义不知道“我的”按钮是什么变量。 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.我正在使用
.after
脚本,因此enable_btn
def1
在调用 def1 后运行 1 秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.