0
votes

Hi let me start off by saying I'm a complete novice at this, recently started learning Python and am currently learning Tkinter to make some GUI applications.

I'm building a really simple stopwatch application here with start, stop, and clear buttons. Everything works except I'm having an issue with a function randomly being called by another button that isn't supposed to call it.

The boolean start is set to True by default. When you click the "Start" button, it calls the starter function that checks that start is True, and if it is, it begins a counter. When you press the "Stop" button, it calls a stopper function that simply stops the counter by changing start to False. When you click the "Clear" button, it calls a clearer function that resets the second and minute values and changes start back to being True (so that IF the counter function is called by the "Start" button it can continue).

The issue I'm having is that if you click "Stop" and afterwards click "Clear" quickly, it for some reason activates the counter function and starts counting. I don't understand why this is happening, because in theory it should only call that function if the "Start" button is pressed.

I've tried messing with different boolean triggers like "start is not False" and stuff like that but no luck.

secs = -1
mins = 0
start = True

def counter():
global secs, mins, start
    if start is True:
        seclabel.after(1000, counter)
        secs += 1
        if secs == 60:
            secs = 0
            mins += 1
        minlabel.configure(text=str(mins))
        seclabel.configure(text=str(secs))
        startbutt.configure(state=DISABLED, bg="grey")
        stopbutt.configure(state=NORMAL, bg="red")

def stopper():
    global start
    start = False
    stopbutt.configure(state=DISABLED, bg="grey")
    clearbutt.configure(state=NORMAL, bg="blue")


def clearer():
    global start, secs, mins
    secs = -1
    mins = 0
    minlabel.configure(text="0")
    seclabel.configure(text="0")
    clearbutt.configure(state=DISABLED, bg="grey")
    startbutt.configure(state=NORMAL, bg="green")
    start = True

stopbutt = Button(window, text="Stop", bg="grey", fg="white", font="arial 15 bold", command=stopper, state=DISABLED)
stopbutt.grid(row=2, column=1, pady=20, padx=20)

clearbutt = Button(window, text="Clear", bg="grey", fg="white", font="arial 15 bold", command=clearer, state=DISABLED)
clearbutt.grid(row=3, column=1, pady=20, padx=20)

startbutt = Button(window, text="Start", bg="green", fg="white", font="arial 15 bold", command=counter, state=NORMAL)
startbutt.grid(row=2, column=0, pady=20, padx=20)

Ideally the clear button shouldn't touch the counter function, so I don't know what's going on.

1

1 Answers

1
votes

If the "Start" button is clicked, which calls counter() and that does the seclabel.after(1000, counter), then after that you click "Stop", which sets start = False and then afterwards quickly click "Clear" which sets it to start = True, it's quite possible for the global start variable to get reset back to True before counter() is called again by the after() method call 1000 ms later.

The proper way to do to would be to cancel any previous seclabel.after(1000, counter) call as well as set start = False. This can be done by using the universal after_cancel() method.