Time stops when the Python timer button is pressed

Asked 1 years ago, Updated 1 years ago, 297 views

I'd like to make a timer for school special activities.

I made the time count and the buttons I don't know how to stop time when the button is pressed.

I'm applying what I've learned, so I'd like to add a function to stop after I've already used it as much as I can. Is it possible? Don't tell me I have to rip it all off and fix it, right?ㅜ<

I think we can add a countdown function to the click button function, but I don't know what to do.

window = tkinter. Tk()
window.title ("Stopwatch")

def clickButton():
    label1["text"] = "Timer stopped"
cnt = 0
def countUp():
    global cnt
    cnt = cnt + 1
    label2["text"] = cnt
    window.after(1000,countUp)

window.geometry ("500×400")
label1 = tkinter.Label (window, text="타이머 작동중", font=(systen" , 25))
label1.place(x=150, y=160)

button1 = tkinter.Button (window, text="STOP!", font=("System", 50), command=clickButton)
button1.place(x=150, y=250)

label2 = tkinter.Label(text="00",  font=("Times New Roman" , 80))
label2.pack()

window.after (1000, countUp) 
window.mainloop()

python function tkinter

2022-12-23 15:55

1 Answers

I think it would be good to control the after function, which is simply supposed to go back unconditionally, with a global variable (start in the code below). Only let the timer go when the starter is true! After applying it, I put the else part so that it works when I click it again. I hope it helps you Please press it!

<Code>

starter = True
def clickButton():
    global cnt
    global starter
    if starter:
        starter = False
        label1["text"] = "Timer stopped"
        button1["text"] = "RESUME!"
    else:
        starter = True
        label1["text"] = "Timer is working"
        window.after(1000,countUp)
        button1["text"] = "STOP!"

cnt = 0
def countUp():
    global cnt
    global starter
    if starter:
        cnt = cnt + 1
        label2["text"] = cnt
        window.after(1000,countUp)


2022-12-23 18:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.