so, i got this code, what it does is that it counts down from 20 minutes, is there a way to do so it counts from 0 to 20 min in that same gui?

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk
import time
def count_down():
    for t in range(1200, -1, -1):
        sf = "{:02d}:{:02d}".format(*divmod(t, 60))
        time_str.set(sf)
        root.update()
        time.sleep(1)
root = tk.Tk()
time_str = tk.StringVar()
label_font = ('helvetica', 40)
tk.Label(root, textvariable=time_str, font=label_font, bg='white', 
         fg='blue', relief='raised', bd=3).pack(fill='x', padx=5, pady=5)
tk.Button(root, text='Count Start', command=count_down).pack()
tk.Button(root, text='Count Stop', command=root.destroy).pack()
root.mainloop()

Recommended Answers

All 4 Replies

Do you mean like this?

import time
run = input("Start? Y/N> ")
secs = 0
#if user's input starts with 'y' / 'Y' start counting
if run.upper().startswith('Y'):
    # Loop until we reach 20 minutes running
    while secs != 1200:
        print (">>>>>>>>>>>>>>>>>>>>>", secs)
        # Sleep for a second
        time.sleep(1)
        # Increment the second total
        secs += 1

Change line 9 to ...
for t in range(0, 1200+1):

You should use after
Tkinter is running an infinite loop(the event loop).
When you use a while loop and time sleep,then this can lock up(block GUI).

That's why all GUI toolkit has some kind of timer/schedule/thread task,
that dont interfer with the running event loop.

In Wxpython that i have used most,there is wx.Timer, wx.CallLater.
Here is an example i found with Tkinter,that use this method.

import Tkinter as tk

class ExampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.label = tk.Label(self, text="", width=10)
        self.label.pack()
        self.remaining = 0
        self.countdown(10)

    def countdown(self, remaining = None):
        if remaining is not None:
            self.remaining = remaining    
        if self.remaining <= 0:
            self.label.configure(text="time's up!")
        else:
            self.label.configure(text="%d" % self.remaining)
            self.remaining = self.remaining - 1
            self.after(1000, self.countdown)

if __name__ == "__main__":
    app = ExampleApp()
    app.mainloop()

thanks to all who helped me,the answer i was looking for was vegaseats.
but all other answers were also good :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.