Command function for button "resets"
<p>So in my tkinter python program I am calling on a command when a button is clicked. When that happens it runs a function but in the function I have it set a label to something on the first time the button is clicked and after that it should only update the said label. Basically after the attempt it changes the attempt to 1 ensuring the if statement will see that and not allow it to pass. However it keeps resetting and I don't know how to stop it. When you click the button no matter first or third the button resets and proof of that occurs because the h gets printed. It's as if the function restarts but it shouldn't since it's a loop for the GUI.</p>

<pre><code>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&gt;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
</code></pre>

<p>Also for those of you who saw my previous question I still need help with that I just also want to fix this bug too. </p>

<p>At beginning of function <code>fight</code> you set <code>attempt = 0</code> so you reset it. </p>

<p>Besides <code>attempt</code> is local variable. It is created when you execute function <code>fight</code> and it is deleted when you leave function <code>fight</code>. You have to use global variable (or global <code>IntVar</code>)</p>

<pre><code>attempt = 0

def fight():
    global attempt
</code></pre>

<p>BTW: of you use only values <code>0</code>/<code>1</code> in <code>attempt</code> then you can use <code>True</code>/<code>False</code>.      </p>

<pre><code>attempt = False

def fight():
    global attempt

    ...

    if not attempt:

       attempt = True
</code></pre>

python tkinter