简体   繁体   中英

Python/tkinter timer doesn't update label --- Newbie

I'm new to all of this so please bear with me, the code is pretty straight forward, the timer component itself was tested seperately and it works, tying it with tkinter either gives no update to the label, or it renders the application not responding. Please help. From what i understand, i create mylabel, place it in the root. Instead of print in the python code, i use mylabel.config to update the value, the math checks out, but the gui still doesn't update. The mylabel.after is at the end of every config. The app just crashes now I don't know why, please elaborate if u can.

from tkinter import *
import time
from playsound import playsound


root = Tk()
root.title("Timer")
root.geometry("600x400")
hours = Entry(root)
hours.insert(0,"Enter hours")
hours.grid(row=0, column =0)
brr = Label(root, text=":").grid(row=0, column =1)
minutes = Entry(root)
minutes.insert(0,"Enter minutes")
minutes.grid(row=0, column =2)
brrr = Label(root, text=":").grid(row=0, column =3)
seconds = Entry(root)
seconds.insert(0,"Enter seconds")
seconds.grid(row=0, column =4)
mylabel = Label(root, text=("Hello! there"))
mylabel.grid(row=3, column=2)


    
def zooda():
    global h
    global m
    global s
    h = int(hours.get())
    s = int(seconds.get())
    m = int(minutes.get())
    mylabel.config(text=(h,":",m,":",s))
    hours.delete(0, END)
    minutes.delete(0, END)
    seconds.delete(0, END)
    if s!=0:
        while s>-1:
            s-=1
            time.sleep(1)
            mylabel.config(text=(h , ":" , m , ":" , s))
            mylabel.after(1000, zooda)
            if s==0:
                if m!=0:
                    m-=1
                    s=59
                    time.sleep(1)
                    mylabel.config(text=( h , ":" , m , ":" , s))
                    mylabel.after(1000, zooda)
                else:
                    if h!=0:
                        h-=1
                        m=59
                        s=59
                        time.sleep(1)
                        mylabel.config(text=( h , ":" , m , ":" , s))
                        mylabel.after(1000, zooda)
                        continue
                    else:
                        time.sleep(1)
                        mylabel.config(text=( "Time's Over"))
                        mylabel.after(1000, zooda)
                        playsound('/Users/commo/Desktop/beep.wav')
                        break
    else:
        if h>=1:
            h-=1
            m=59
            s=60
            while s>-1:
                s-=1
                time.sleep(1)
                mylabel.config(text=( h , ":" , m , ":" , s))
                mylabel.after(1000, zooda)
                if s==0:
                    if m!=0:
                        m-=1
                        s=59
                        time.sleep(1)
                        mylabel.config(text=( h , ":" , m , ":" , s))
                        mylabel.after(1000, zooda)
                    else:
                        if h!=0:
                            h-=1
                            m=59
                            s=59
                            time.sleep(1)
                            mylabel.config(text=( h , ":" , m , ":" , s))
                            mylabel.after(1000, zooda)
                            continue
                        else:
                            time.sleep(1)
                            mylabel.config(text=( "Time's Over"))
                            mylabel.after(1000, zooda)
                            playsound('/Users/commo/Desktop/beep.wav')
                            break
        else:
            s=60
            m-=1
            while s>-1:
                s-=1
                time.sleep(1)
                mylabel.config(text=( h , ":" , m , ":" , s))
                mylabel.after(1000, zooda)
                if s==0:
                    if m!=0:
                        m-=1
                        s=59
                        time.sleep(1)
                        mylabel.config(text=( h , ":" , m , ":" , s))
                        mylabel.after(1000, zooda)
                    else:
                        if h!=0:
                            h-=1
                            m=59
                            s=59
                            time.sleep(1)
                            mylabel.config(text=( h , ":" , m , ":" , s))
                            mylabel.after(1000, zooda)
                            continue
                        else:
                            time.sleep(1)
                            mylabel.config(text=( "Time's Over"))
                            mylabel.after(1000, zooda)
                            playsound('/Users/commo/Desktop/beep.wav')
                            break
 
button = Button(root, text="Start Timer", command=lambda:[ zooda()])
button.grid(row=1,column=2) 
root.mainloop()

You can use the ' after ' method. while and for loops stop your program, time does that too. it makes sense to use what is already there. eg function calls

from tkinter import Tk, Label

root = Tk()


def type_writer(text: str, count=0):
    if count < len(text):
        lbl.config(text=text[:count+1])
        root.after(100, lambda : type_writer(text, count + 1))


txt = "Hello World"

lbl = Label()
lbl.pack()

type_writer(txt)

root.mainloop()



The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM