按钮的命令功能"重置"

时间:2016-10-19 00:11:06

标签: python tkinter

所以在我的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

对于那些看过我上一个问题的人,我仍然需要帮助,我也想修复这个bug。

1 个答案:

答案 0 :(得分:1)

在功能fight开始时设置attempt = 0,然后重置它。

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

attempt = 0

def fight():
    global attempt

BTW:您只使用0中的值1 / attempt,然后您可以使用True / False

attempt = False

def fight():
    global attempt

    ...

    if not attempt:

       attempt = True