如何通过将所有购买按钮附加到 if 语句来禁用它们(例如对于第一个购买按钮)
if counter.get()>15:
state="disabled
else:
state="enabled"
我尝试了 20-50 种不同的东西,甚至将我的整个代码更改为网格样式。我只想要我拥有的代码和一种禁用按钮的简单方法,直到满足条件。最难弄清楚这一点。
import tkinter
root = tkinter.Tk()
root.geometry("480x320")
root.title("Button Clicker")
counter = tkinter.IntVar()
Ant = tkinter.IntVar()
autoclicker = 0
def onClick(event=None):
counter.set(counter.get() + 1)
def buyAnt(event=None):
if counter.get()-10>=0:
counter.set(counter.get() -10) ,Ant.set(Ant.get() + 1)
def buyCat(event=None):
if counter.get()-50>=0:
counter.set(counter.get() -50)
def buyDog(event=None):
if counter.get()-100>=0:
counter.set(counter.get() -100)
def buyVillager(event=None):
if counter.get()-200>=0:
counter.set(counter.get() -200)
def autoclick(event=None):
counter.set(counter.get()+1)
root.after(1000, autoclick )
def buyAutoClicker(event=None):
if counter.get()-15>=0:
counter.set(counter.get() -15), autoclicker + 1,
root.after(1000, autoclick)
tkinter.Label(root, textvariable=counter).pack()
tkinter.Label(root, textvariable=Ant).pack()
tkinter.Button(root, text="Click", command=onClick, fg="dark green",
bg = "white").pack()
btn2 = tkinter.Button(root, text="Buy AutoClicker",
command=buyAutoClicker, fg="dark green", bg = "white").pack()
tkinter.Button(root, text="Buy Cat", command=buyCat, fg="dark green",
bg = "white").pack()
tkinter.Button(root, text="Buy Dog", command=buyDog, fg="dark green",
bg = "white").pack()
tkinter.Button(root, text="Buy Villager", command=buyVillager,
fg="dark green", bg = "white").pack()
root.mainloop()
我希望点击(正常)购买 AutoClicker(在计数器>15 之前灰显)