#🔒 Tkinter Interface Freezes When Running Alarm Function

11 messages · Page 1 of 1 (latest)

warm blade
#

I'm working on an assignements thats a reminder app using Python's Tkinter and win10toast for desktop notifications. I'm encountering an issue where the Tkinter interface becomes unresponsive after I press the button to activate the alarm function. The program does successfully display the toast notification when the specified time is reached, but the GUI freezes and does not respond to any input. Below is the relevant part of my code:

from tkinter import *
from win10toast import ToastNotifier
import time
import datetime

gluggi = Tk()
gluggi.geometry("400x200")

def toaststart():
notifier= ToastNotifier()
notifier.show_toast("skilaboð frá tölvu...", "ÁMINNING")

def alarm():
while True:
set_alarm_time = f"{hour.get()}:{minut.get()}:{sekund.get()}"
time.sleep(1)
current_time = datetime.datetime.now().strftime("%H:%M:%S")
if current_time == set_alarm_time:
toaststart()

Label(gluggi, text="áminning", font=("Helvetica 20 bold"), fg="red").pack(pady=10)
Label(gluggi, text="velja tíma", font=("Helvetica 15 bold")).pack()
rammi= Frame(gluggi)
rammi.pack()
hour=StringVar(gluggi)
hours =[f"{i:02}" for i in range(24)]
hour.set(hours[0])
hrs = OptionMenu(rammi, hour, *hours)
hrs.pack(side=LEFT)
minut=StringVar(gluggi)
mins =[f"{i:02}" for i in range(60)]
minut.set(mins[0])
min = OptionMenu(rammi, minut, *mins)
min.pack(side=LEFT)
sekund=StringVar(gluggi)
seks =[f"{i:02}" for i in range(60)]
sekund.set(seks[0])
sek = OptionMenu(rammi, sekund, *seks)
sek.pack(side=LEFT)
Button(gluggi, text="virkja áminningu", font=("Helvetica 15"), command=alarm).pack(pady=20)
gluggi.mainloop()

i get this error: WNDPROC return value cannot be converted to LRESULT
TypeError: WPARAM is simple, so must be an int object (got NoneType)

The GUI freezes right after I press the "virkja áminningu" button. How can I prevent the GUI from freezing while still using the alarm() function as intended?

raven forgeBOT
#

@warm blade

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hallow cloak
#

it will freeze

#

you also cannot use a while loop

little plover
#

the simple option would be running this in a separate thread.

zenith sinew
#

or user tkinter features, e.g. the .after() method to schedule a function call after some time

#

gluggi.after(1000, alarm)

warm blade
#

the after() methood worked for me, thanks a lot

def alarm():
set_alarm_time = f"{hour.get()}:{minut.get()}:{sekund.get()}"
current_time = datetime.datetime.now().strftime("%H:%M:%S")
if current_time == set_alarm_time:
toaststart()
gluggi.after(1000, alarm)

Button(gluggi, text="virkja áminningu", font=("Helvetica 15"), command=lambda: gluggi.after(1000, alarm)).pack(pady=20)

raven forgeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.