0

I just started my first project with Python, not only Python but also inside the world of coding. My project is to help my productivity to improve during this quaratine using POMODORO technique. My problem is to display the count down clock in the tk AND also making the count to be att and displayed.

def task():
    minutes = 2
    min = 5
    seconds = 0
    sec = 0
    while True:
        timer = str(minutes) + str(min) + ':' + str(seconds) + str(sec)
        label = tk.Label(frame, text=timer, bg="green")
        label.pack()
        if seconds == 0 and sec == 0 and min == 0:
            seconds = 5
            sec = 9
            min = 10
            minutes = minutes - 1
        if seconds == 0 and sec == 0:
            seconds = 5
            sec = 10
            min = min - 1
        if sec == 0:
            sec = 10
            seconds = seconds - 1
        sec = sec - 1
        time.sleep(1)
        if minutes == 0 and seconds == 0 and sec == 0 and min == 0:
            print('ご苦労様でした ! ! !')
            break

root = tk.Tk()
root.title('POMODORO')
canvas = tk.Canvas(root, height=700, width=700, bg="green")
canvas.pack()
frame = tk.Frame(root, bg="white")
frame.place(relwidth = 0.8, relheight = 0.8, relx=0.1, rely=0.1)
task_button = tk.Button(frame,text="Start Task",padx = 10, pady=5, fg ="black", bg="green", command=task)
task_button.pack()
root.mainloop()

I found it difficult for my current level but every step done make me happier :)

Thanks for all your support already!

  • 3
    Does this answer your question? [Making a countdown timer with Python and Tkinter?](https://stackoverflow.com/questions/10596988/making-a-countdown-timer-with-python-and-tkinter) – Gustav Rasmussen Jun 07 '20 at 20:48

1 Answers1

3

Tkinter already has an infinite loop running (the event loop), and a way to schedule things to run after a period of time has elapsed (using after). You can take advantage of this by writing a function that calls itself once a second to update the display. You can use a class variable to keep track of the remaining time.

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()
Ritik Kumar
  • 661
  • 5
  • 16