首页 > 解决方案 > 如何使用我当前的代码禁用和启用选项卡

问题描述

我正在制作游戏,并希望禁用“技能树”选项卡,直到重生计数器为 +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 之后不会禁用第二个选项卡。

标签: pythonpython-3.xtkinter

解决方案


推荐阅读