按钮“复位”命令功能

2024-10-01 04:48:07 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,在我的TkinterPython程序中,我在单击按钮时调用一个命令。当这种情况发生时,它会运行一个函数,但在函数中,我让它在第一次单击按钮时设置一个标签,然后它应该只更新所说的标签。基本上,在尝试之后,它将尝试更改为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

同样对于那些看到我之前问题的人,我仍然需要帮助,我也只是想修复这个bug


Tags: rightgetifleft按钮totalsetelif
1条回答
网友
1楼 · 发布于 2024-10-01 04:48:07

在函数fight的开头,您设置了attempt = 0,所以您重置了它

另外attempt是局部变量。它在执行函数fight时创建,在离开函数fight时删除。必须使用全局变量(或全局IntVar

attempt = 0

def fight():
    global attempt

顺便说一句:如果你只在attempt中使用值0/1,那么你可以使用True/False

attempt = False

def fight():
    global attempt

    ...

    if not attempt:

       attempt = True

相关问题 更多 >