Home > database >  Pause and Resume for tkinter pomodoro clock
Pause and Resume for tkinter pomodoro clock

Time:12-21

I've been working on a python tkinter pomodoro clock. About everything else works perfectly except for the resume button. This is my code:

from tkinter import *
window = Tk()
# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
rep = 0
timer_var = None
count_min = 0
count_sec = 0

#----------------------------Pause----------------------------------------#


def pause():
    global timer_var
    if pause_button['text'] == 'pause':
        window.after_cancel(timer_var)
        pause_button.config(text='resume')
    if pause_button['text'] == 'resume':
        countdown(count_min * 60   count_sec)
        timer_function()


# ---------------------------- TIMER RESET ------------------------------- #


def reset_timer():
    global timer_var
    global rep
    window.after_cancel(timer_var)
    canvas.itemconfig(countdown_text, text=f"")
    timer_label.config(text='Timer', fg=GREEN)
    marks = ''
    checkmark.config(text=marks)
    rep = 0
    start_button.config(state='active')

# ---------------------------- TIMER MECHANISM ------------------------------- #


def timer_function():
    global rep
    if int(count_min) * 60   int(count_sec) == 0:
        rep  = 1
    start_button.config(state='disabled')
    if rep in (1, 3, 5, 7):  # instead of saying rep == smth or rep ==smth2 use this
        timer_label.config(text='work', fg=GREEN)
        countdown(20)
    elif rep in (2, 4, 6):
        timer_label.config(text='short break', fg=RED)
        countdown(5)
    elif rep == 8:
        timer_label.config(text='long break', gf=RED)
        countdown(10)
    elif rep == 9:
        rep = 0


# ---------------------------- COUNTDOWN MECHANISM ------------------------------- #


def countdown(count):
    global count_min
    global count_sec
    count_min = count // 60
    count_sec = count - count_min * 60
    if count_sec < 10:
        count_sec = '0'   str(count_sec)
    canvas.itemconfig(countdown_text, text=f"{count_min}:{count_sec}")
    if count > 0:
        global timer_var
        timer_var = window.after(1000, countdown, count -
                                 1)  # this part is recursion
    else:
        timer_function()  # cant use loop so have to call the function again and again
        work_session_num = rep // 2
        marks = ''
        for _ in range(work_session_num):
            marks  = '✓'
            checkmark.config(text=marks)


# ---------------------------- UI SETUP ------------------------------- #

window.title('pomodoro')
window.config(padx=90, pady=60, bg=YELLOW)
file_location = PhotoImage(file='day28/tomato.png')

timer_label = Label(text='Timer', width=12, font=(
    FONT_NAME, 60, 'normal'), fg=GREEN, bg=YELLOW)
timer_label.grid(row=0, column=1)

start_button = Button(text='start', command=timer_function, state='active')
start_button.grid(row=2, column=0)

reset_button = Button(text='reset', command=reset_timer)
reset_button.grid(row=2, column=2)

pause_button = Button(text='pause', command=pause)
pause_button.grid(row=2, column=1)

checkmark = Label(bg=YELLOW, fg=GREEN,  font=(FONT_NAME, 15, 'bold'))
checkmark.grid(row=3, column=1)

canvas = Canvas(width=200, height=224, bg=YELLOW, highlightthickness=0)
canvas.create_image(100, 112, image=file_location)
canvas.grid(row=1, column=1)

countdown_text = canvas.create_text(100, 130, fill='white',
                                    font=(FONT_NAME, 35, 'bold'))

window.mainloop()

The 'pause function' has some issues. I can get it the stopwatch to pause, but when resumes, the stopwatch starts increasing and decreasing randomly. Help would be greatly appreciated.

CodePudding user response:

I tried running your code and added some print statements. What seems to happen is that when you 'pause' or 'resume' the countdown is not actually stopped. At first, your code calls countdown() every second. Then when I pause the counter, it's called twice every second. Your countdown function isn't actually canceled.

If you look carefully at the "random" numbers you see when you hit resume again, you notice it isn't actually random. You see two counters counting down at the same time. For example, you will see 18, 10, 17, 9, 16, 8.

Counter 1: 18, 17, 16 and Counter 2: 10, 9, 8.

CodePudding user response:

I don't know how is the pause working for you, because you used if instead of elif, so when it turns into “resume” it would just kept running.

In addition, I don’t suggest you to directly use the text on the button to declare whether if the clock it’s paused or running, and instead use another variable.

  • Related