所以在我的tkinter python程序中,当按钮被单击时,我调用命令。当这种情况发生时,它运行一个函数,但在函数中,我有它设置一个标签的东西在第一次按钮被点击,之后,它应该只更新所述标签。基本上,在尝试之后,它将尝试更改为1,确保if语句将看到它并且不允许它通过。然而,它一直复位,我不知道如何阻止它。当你第一次或第三次点击按钮时,按钮会重置,因为h被打印出来。这就好像函数重新启动,但它不应该,因为它是GUI的循环。
def fight(): #Sees which one is stronger if user is stronger he gets win if no he gets loss also displays enemy stats and removes used characters after round is finished
try:
attempt=0
namel = ""
namer=""
left = lbox.curselection()[0]
right = rbox.curselection()[0]
totalleft = 0
totalright = 0
if left == 0:
namel = "Rash"
totalleft = Rash.total
elif left==1:
namel = "Untss"
totalleft = Untss.total
elif left==2:
namel = "Illora"
totalleft = 60+35+80
if right == 0:
namer = "Zys"
totalright = Zys.total
elif right==1:
namer = "Eentha"
totalright = Eentha.total
elif right==2:
namer = "Dant"
totalright = Dant.total
lbox.delete(lbox.curselection()[0])
rbox.delete(rbox.curselection()[0])
print(namel)
print(namer)
if attempt == 0:
wins.set("Wins")
loss.set("Loss")
print("h")
attempt=1
if (totalleft>totalright):
wins.set(wins.get()+"n"+namel)
loss.set(loss.get()+"n"+namer)
else:
wins.set(wins.get()+"n"+namer)
loss.set(loss.get()+"n"+namel)
except IndexError:
pass
对于那些看到我之前的问题的人,我仍然需要帮助,我只是也想修复这个错误。
在函数fight
的开始,您设置了attempt = 0
,因此您重置了它。
attempt
为局部变量。它是在执行函数fight
时创建的,在离开函数fight
时删除。您必须使用全局变量(或全局IntVar
)
attempt = 0
def fight():
global attempt
BTW:如果你在attempt
中只使用0
/1
,那么你可以使用True
/False
。
attempt = False
def fight():
global attempt
...
if not attempt:
attempt = True