如何使用我的当前代码禁用和启用选项卡

时间:2019-05-17 03:38:35

标签: python python-3.x tkinter

我正在制作游戏,希望禁用“技能树”选项卡,直到重生计数器为+1为止。我希望禁用该选项卡,然后在满足该条件时启用。我希望它可以为玩家提供一种解锁方式。

我尝试过我之前建议的禁用/启用按钮的方法,但是我认为它对选项卡的作用不同。

例如:(虽然不起作用)

def unlock1(event=None)
   if rebirth.get()>=1:
     tab2.configure(state="disabled")
   else:
     tab2.configure(state="normal")

这是我的代码:

import tkinter as tk                    
from tkinter import ttk

root = tk.Tk()                               
root.geometry("480x320")
root.title("Python GUI")                 


tabControl = ttk.Notebook(root)          
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tabControl.add(tab1, text="Click")
tabControl.add(tab2, text="Skill Tree", state="disabled")
tabControl.pack(expand=1, fill="both")  


counter = tk.IntVar()
Ant = tk.IntVar()
autoclicker = tk.IntVar()
rebirth = tk.IntVar()

def onClick(event=None):
    counter.set(counter.get() + 1)
def switch1(name, index, op):
    print("called")
    if counter.get() <= 14:
        btn2.configure(state = 'disabled')
    else:
        btn2.configure(state = 'normal')
def switch2(name, index, op):
    if counter.get() <= 49:
        btn3.configure(state = 'disabled')
    else:
        btn3.configure(state = 'normal')

def switch3(name, index, op):
    if counter.get() <= 99:
        btn4.configure(state = 'disabled')
    else:
        btn4.configure(state = 'normal')

def switch4(name, index, op):
    if counter.get() <= 199:
        btn5.configure(state = 'disabled')
    else:
        btn5.configure(state = 'normal')

counter.trace("w", switch1)
counter.trace("w", switch2)
counter.trace("w", switch3)
counter.trace("w", switch4)

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):
    if autoclicker.get()>0:
        counter.set(counter.get()+1)
        root.after(1000, autoclick)
def buyAutoClicker(event=None):
    if counter.get()-15>=0:
        counter.set(counter.get() -15), autoclicker.set(autoclicker.get() 
+ 1), root.after(1000, autoclick)
def Rebirth(event=None):
    if counter.get()-10>=0:
        counter.set(0), rebirth.set(rebirth.get()+1), autoclicker.set(0)




tk.Label(tab1, textvariable=counter).pack()
tk.Label(tab1, textvariable=rebirth).pack()
tk.Button(tab1, text="Click", command=onClick, fg="dark green", bg = 
"white").pack()
btn2 = tk.Button(tab1, text="Buy AutoClicker", command=buyAutoClicker, 
fg="dark green", bg = "white", state = "disabled")
btn2.pack()
btn3 = tk.Button(tab1, text="Buy Cat", command=buyCat, fg="dark green", bg 
= "white", state = "disabled")
btn3.pack()
btn4 = tk.Button(tab1, text="Buy Dog", command=buyDog, fg="dark green", bg 
= "white", state = "disabled")
btn4.pack()
btn5 = tk.Button(tab1, text="Buy Villager", command=buyVillager, fg="dark 
green", bg = "white", state = "disabled")
btn5.pack()
btn6 = tk.Button(tab1, text="Rebirth", command=Rebirth, fg="dark green", 
bg = "white") ##state = "disabled"
btn6.pack()


root.mainloop()

我希望在rebirth.get()> = 1之后不会禁用第二个选项卡。

0 个答案:

没有答案