Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

python - How do I make a simple countdown time in tkinter?

I am making a simple countdown timer in minutes. I can't seem to display the countdown in text label. Can someone help me?

import tkinter as tk
import time

def countdown(t):
    while t:
        mins, secs = divmod(t, 60)
        timeformat = '{:02d}:{:02d}'.format(mins, secs)
        print(timeformat, end='
')
        label.config(text=timeformat)
        time.sleep(1)
        t -= 1

root = tk.Tk()
label = tk.Label(root,text="Time")
label.pack()
button = tk.Button(root,text = "click here", command=countdown(60)).pack()

root.mainloop()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, instead of using this

button = tk.Button(root,text = "click here", command=countdown(60)).pack()

You have to use lambda to specify arguments

button = tk.Button(root,text = "click here", command=lambda:countdown(60)).pack()

Then you have to update root each time you update the label

def countdown(t):
while t:
    mins, secs = divmod(t, 60)
    timeformat = '{:02d}:{:02d}'.format(mins, secs)
    print(timeformat, end='
')
    label.config(text=timeformat)
    root.update() #This way your program will display the numbers.
    time.sleep(1)
    t -= 1

Also I recommend to use Threads, to be able to use other buttons while your program is running.

Requested code:

import threading
import tkinter as tk
import time
import sys

runtime = 300 #In seconds, you can change this

class CountdownApp:
    def __init__(self, runtime):
        self.runtime = runtime
        self._createApp()
        self._packElements()
        self._startApp()

    def _createApp(self):
        self.root = tk.Tk()

    def _packElements(self):
        self.label = tk.Label(self.root,text="Time")
        self.label.pack()
        self.button = tk.Button(self.root,text = "click here", command=self.startCounter)
        self.button.pack()

    def countdown(self):
        self.t = self.runtime
        self.button.config(state='disabled')
        while self.t > -1:
            self.mins, self.secs = divmod(self.t, 60)
            self.timeformat = '{:02d}:{:02d}'.format(self.mins, self.secs)
            self.label.config(text=self.timeformat)
            self.root.update()
            time.sleep(1)
            self.t -= 1
        self.label.config(text='Time')
        self.root.update()
        self.button.config(state='normal')

    def startCounter(self):
        threading.Thread(target=self.countdown).start()

    def _startApp(self):
        self.root.mainloop()


CountdownApp(runtime)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...