简体   繁体   中英

How do I get this timer to count down properly?

I am trying to make a simple gui where you can press a button to start aa timer, and see it count down, similar to https://web.5217.app/ , But I cannot get the timer to display in the gui, any help would be appreciated.

Also this is my first question so I may have done something wrong.

from tkinter import Tk, Button, DISABLED, Label, ACTIVE
import time

#main window configuration
root = Tk()
root.title ("PyDoro")
root.geometry ("400x400")
root.configure(bg = "#383838") 

#colours for the text
Colour1 = "#c4c4c4"
Colour2 = "#292828"

def Date(): #Defines the date for displaying under the clock
    day = time.strftime("%d")
    month = time.strftime("%b") # %B can be used for full month
    year = time.strftime("%Y")

    Calendar.config (text= day + " " + month + " " + year)

def clock(): # for creating the clock
    tizo = time.strftime("%X") #Find the time for your locale
    Time.config (text = tizo)
    Time.after (1000, clock)
    Date() #calling the Date because it makes sense to do it here

def Stop():
    print ("nothing")

def Start():
    time_left = (50)
    Stop.config (state = ACTIVE)
    timer = Label (root, text = time_left)
    timer.pack()

    for i in range (50):
        timer.config (text = time_left)
        Start.after (1000) # this waits for 1 minute (60000 miliseconds)
        #print (i) # This is just for debugging
        time_left = time_left - 1 
        print (time_left)
        




Start = Button (root, text = "Start!", fg = Colour1, bg = Colour2, padx = 40, command = Start)
Stop = Button (root, text = "stop", fg = Colour1, bg = Colour2, state = DISABLED)



Time = Label (root, text="", font = ("Helvetica", 50), fg = Colour1, bg = "#383838")
Time.pack (pady = 5)

Calendar = Label (root, font = ("Helvetica", 12), fg = Colour1, bg = "#383838")
Calendar.pack (pady = 5)

Start.pack (pady = 10)

Stop.pack (pady = 10)


clock()
root.mainloop() #Runs the program

Replace your Start() function with the following code:

def Start():
    time_left = (50)
    Stop.config (state = ACTIVE)
    timer = Label (root, text = time_left)
    timer.pack()
    def update(time_left):
        timer['text'] = time_left
        if time_left > 0:
            root.after(1000, update, time_left-1)
    update(time_left)

After you create your label, the program calls a function called update, which sets the text of the timer label to time_left . It will then call root.after if time_left is greater than 0, and it passes time_left -1 back into the update function. This will make the timer countdown until it reaches 0.

The reason the timer Label isn't being shown is because the display is never given a chance to update. To fix that, try using the Start() function shown below which calls the universal widget method update_idletasks() to update it after it's been changed.

def Start():
    time_left = 50
    Stop.config(state=ACTIVE)
    timer = Label(root, text=time_left)
    timer.pack()

    for i in range(50):
        timer.config(text=time_left)
        time_left -= 1
        root.update_idletasks()  # Update display.
        root.after(1000)  # Pause for 1000 milliseconds (1 second).

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