How do I make a simple countdown time in tkinter?

2024/10/9 20:21:23

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 timedef countdown(t):while t:mins, secs = divmod(t, 60)timeformat = '{:02d}:{:02d}'.format(mins, secs)print(timeformat, end='\r')label.config(text=timeformat)time.sleep(1)t -= 1root = tk.Tk()
label = tk.Label(root,text="Time")
label.pack()
button = tk.Button(root,text = "click here", command=countdown(60)).pack()root.mainloop()
Answer

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='\r')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 sysruntime = 300 #In seconds, you can change thisclass CountdownApp:def __init__(self, runtime):self.runtime = runtimeself._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.runtimeself.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 -= 1self.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)
https://en.xdnf.cn/q/118544.html

Related Q&A

Embed one pdf into another pdf using PyMuPDF

In need of help from learned people on this forum. I just want to embed one pdf file to another pdf file. So that when I go to the attachment section of the second file I can get to see and open the fi…

How to fix - TypeError: write() argument must be str, not None

Here is my code - sentence = input("Enter a sentence without punctuation") sentence = sentence.lower() words = sentence.split() pos = [words.index(s)+1 for s in words] hi = print("This s…

Is there a way to get source of a python file while executing within the python file?

Assuming you have a python file like so#python #comment x = raw_input() exec(x)How could you get the source of the entire file, including the comments with exec?

How can I stop find_next_sibling() once I reach a certain tag?

I am scraping athletic.net, a website that stores track and field times. So far I have printed event titles and times, but my output contains all times from that season rather than only times for that …

How can I make a map editor?

I am making a map editor. For 3 tiles I have to make 3 classes: class cloud:def __init__(self,x,y,height,width,color):self.x = xself.y = yself.height = heightself.width = widthself.color = colorself.im…

Counting total number of unique characters for Python string

For my question above, Im terribly stuck. So far, the code I have come up with is:def count_bases():get_user_input()amountA=get_user_input.count(A)if amountA == 0:print("wrong")else:print (&q…

adding a newly created and uploaded package to pycharm

I created a package (thompcoUtils) on test.pypi.org and pypi.org https://pypi.org/project/thompcoUtils/ and https://test.pypi.org/project/thompcoUtils/ show the package is installed in both the test an…

Using builtin name as local variable but also as builtin [duplicate]

This question already has answers here:UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)(14 answers)Closed 1 year ago.I have the following f…

How to print the results of a SQLite query in python?

Im trying to print the results of this SQLite query to check whether it has stored the data within the database. At the moment it just prints None. Is there a way to open the database in a program like…

python sort strings with leading numbers alphabetically

I have a list of filenames, each of them beginning with a leading number:10_file 11_file 1_file 20_file 21_file 2_file ...I need to put it in this order:1_file 10_file 11_file 2_file 21_file 22_file ..…