Skip to content
Advertisement

Python Tkinter count-down GUI crashing when i start the Count-Down with a button. (app not answering)

I am making a count-down GUI with Tkinter in Python 3.10 and I’m making a slider that sets the minutes, 2 labels that display the minutes and seconds and a button to start the timer.

The problem is that it crashes when I click the start timer button, The strange thing is that it doesn’t give me any error messages it just doesn’t respond/crashes the GUI.

I’m using functions with long loops and I think the problem is with the loops.

Both the slider and Button calls functions

I’m a beginner so I know that the code isn’t so good but I’m trying my best. Please Help Me :)

Python Code:

from time import sleep
from tkinter import *


root = Tk()
root.geometry("300x400")
root.title("Countdown Timer")

time = 0
time_2 = 0

running = False
def clear_field():
    clock_text_min.delete(1.0, "end")
    clock_text_sec.delete(1.0, "end")
    min_slider.set(30)


def before_start_timer(time):
    global time_2
    time_2 = time
    time = int(time)
    time_3 = str(time)
    counter = 0
    while (not running) and (counter < 10):
        clock_text_min.config(text=time_3)
        counter += 1

def start_timer(time):
    global running
    running = True
    time = int(time)
    while time >= 0:
        clock_text_min.config(text=time)
        sec_time = 60
        for _ in range(60):
            clock_text_sec.config(text=sec_time)
            sleep(1)
            sec_time -= 1
        time -= 1

clock_text_min = Label(root, text="0", font=("Arial", 25))
clock_text_min.pack(padx=20, pady=20)

clock_text_sec = Label(root, text="0", font=("Arial", 25))
clock_text_sec.pack(padx=20, pady=20)

min_slider = Scale(root, from_=1, to=60, orient=HORIZONTAL, command=before_start_timer)
min_slider.set(30)
min_slider.pack(padx=20, pady=20)

start_button = Button(root, text="Start Countdown", font=("Arial", 15), command=lambda: start_timer(time_2))
start_button.pack(padx=20, pady=20)

root.mainloop()

Its a very difficult problem so i haven’t tried anything yet because i dont understand the problem, but as i said i think the problem is in the loops.

I was expecting it to start coundting down from the set number of minutes and when its finished just stop at 0 on both min and sec but it just crashed when i clicked start countdown.

Please help me, it would be really helpfull because this is my second GUI i have ever made without any help :)

code is fixed/question ended

Hi everyone, thanks for all the help, my code is now working by using the after function and the root.update_idletasks() function. thanks

Advertisement

Answer

As JRiggles mentioned sleep and tkinter do not go well. This is because sleep() blocks the main loop of tkinter.

The most common way to solve this issue is to use after() to manage these kind of loops.

Here is a paired down version of your code that uses a combination of after() and a refactored function.

import tkinter as tk

root = tk.Tk()
root.geometry("300x200")
root.title("Countdown Timer")


def start_timer(time):
    if time >= 0:
        clock_text_sec.config(text=time)
        time -= 1
        # you can supply a variable after the function call with after.
        root.after(1000, start_timer, time)
        # you can also use a lambda to do the same thing
        # root.after(1000, lambda: start_timer(time))


clock_text_sec = tk.Label(root, text="0", font=("Arial", 25))
clock_text_sec.pack(padx=20, pady=20)
tk.Button(root, text="Start Countdown", font=("Arial", 15), command=lambda: start_timer(60)).pack(padx=20, pady=20)

root.mainloop()
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement