Python Forum

Full Version: tkinter freeze
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey, I'm new to python and I'm trying to code a alarms clock program.
for some reason when I'm trying to find if the alarm(input) is equal to the time(tick function), the clock label in the tkinter is getting frozen and the alarm label dosen't show up.
the program still works (if you wait when it froze to when they will be equal is prints the "!!!" part but i don't know how to make the clock label to continue.
this is my code:

from tkinter import *
import time


def new_alarm():
    nw_ala_win = Toplevel()
    nw_ala_win.geometry('200x200')
    nw_ala_win.title("New Alarm")
    ala_txt = StringVar()
    ala_entry = Entry(nw_ala_win, textvariable=ala_txt)
    ala_entry.grid(row=1, column=0)
    enter_button = Button(nw_ala_win, text="Enter", command=lambda: update_alarms(nw_ala_win, ala_txt))
    enter_button.grid(row=1, column=1)


def update_alarms(ala_win, w_ala):
    ala = w_ala.get()
    ala_label = Label(ala_win, text=ala)
    ala_label.grid(row=2, column=0)
    while ala_label.winfo_exists():
        if str(ala) == str(tick()):
            print("!!!")


def quit_root(root):
    root.destroy()


def start_prog():
    menu = Menu(root)
    root.config(menu=menu)

    file_submenu = Menu(menu)
    menu.add_cascade(label="File", menu=file_submenu)
    file_submenu.add_command(label="New Alarm", command=new_alarm)
    file_submenu.add_separator()
    file_submenu.add_command(label="Exit", command=lambda: quit_root(root))

    edit_submenu = Menu(menu)
    menu.add_cascade(label="Edit", menu=edit_submenu)
    edit_submenu.add_command(label="Redo")


def tick():
    time_str = time.strftime("%H:%M:%S")
    clock.config(text=time_str)
    clock.after(200, tick)
    return time_str


root = Tk()

start_prog()
clock = Label(root, font=("times", 100, "bold"), bg="white")
clock.grid(row=0, column=0)
tick()

root.mainloop()

Ok, I managed to find a solution online using callbacks,
here is the new code:
from tkinter import *
import time


def new_alarm():
    nw_ala_win = Toplevel()
    nw_ala_win.geometry('200x200')
    nw_ala_win.title("New Alarm")
    ala_txt = StringVar()
    ala_entry = Entry(nw_ala_win, textvariable=ala_txt)
    ala_entry.grid(row=1, column=0)
    enter_button = Button(nw_ala_win, text="Enter", command=lambda: update_alarms(nw_ala_win, ala_txt))
    enter_button.grid(row=1, column=1)


def update_alarms(ala_win, w_ala):
    ala = w_ala.get()
    ala_label = Label(ala_win, text=ala)
    ala_label.grid(row=2, column=0)

    def callback():
        if str(ala) == str(tick()):
            print("!!!")
        else:
            root.after(1000, callback)
    root.after(1000, callback)


def quit_root(root):
    root.destroy()


def start_prog():
    menu = Menu(root)
    root.config(menu=menu)

    file_submenu = Menu(menu)
    menu.add_cascade(label="File", menu=file_submenu)
    file_submenu.add_command(label="New Alarm", command=new_alarm)
    file_submenu.add_separator()
    file_submenu.add_command(label="Exit", command=lambda: quit_root(root))

    edit_submenu = Menu(menu)
    menu.add_cascade(label="Edit", menu=edit_submenu)
    edit_submenu.add_command(label="Redo")


def tick():
    time_str = time.strftime("%H:%M:%S")
    clock.config(text=time_str)
    clock.after(200, tick)
    return time_str


root = Tk()

start_prog()
clock = Label(root, font=("times", 100, "bold"), bg="white")
clock.grid(row=0, column=0)
tick()

root.mainloop()