0

I was recently trying to create a pomodoro app using tkinter and customtkinter but my app is crashing when I click on the play button.

def counting_down():
    minute_left = int(timer.get()) - 1
    second = 59
    for i in range(60*minute_left):
        time.sleep(1)
        count_label = customtkinter.CTkLabel(root, text=str(minute_left), font=("Times New Roman", 20, "bold"))
        count_label.place(x=120, y=300)
        count_label2 = customtkinter.CTkLabel(root, text=second, font=("Times New Roman", 20, "bold"))
        count_label2.place(x=145, y=300)
        second -= 1
        if second < 0:
            second = 59
            minute_left -= 1
            count_label.destroy()
            count_label = customtkinter.CTkLabel(root, text=str(minute_left), font=("Times New Roman", 20, "bold"))
            count_label.place(x=120, y=300)
        count_label2.destroy()
        count_label2 = customtkinter.CTkLabel(root, text=second, font=("Times New Roman", 20, "bold"))
        count_label2.place(x=145, y=300)
        if minute_left == 0 and second == 0:
            count_label2.destroy()
            count_label.destroy()
            count_label = customtkinter.CTkLabel(root, text="FINISHED", font=("Times New Roman", 20, "bold"))
            count_label.place(x=135, y=300)

this is the function and I use it in my play button

play_button = customtkinter.CTkButton(root, text="PLAY", width=70, command=counting_down)
play_button.place(x=75, y=200)

I tried to click on the play button and then program didn't respond, so I terminate the code then I got an error about a python crash

  • it is likely your program is not responding because you are in a for loop that could be quite long doing "sleeps" all along, with the program never reaching the "mainloop" and hence giving you the sensation that GUI is hang. Use "root.after" to call whatever piece of code you want after X ammount of time without "hanging" your app. On the other hand, if your intention is to update the label text, why dont just updating it over the loop? rather than destroying and creating a new label. This would simplify your code a lot. – mamg2909 Feb 18 '23 at 21:46

0 Answers0