Reputation: 128
I am trying to execute a function called alarm in background while function called clock is running. this is my alarm function. As code inside is complex I am only putting an example.
def Alarm():
while True:
current_time = dt.datetime.now().time()
if timetable[0] == current_time:
print("Its time")
break
clock()
Alarm()
root.mainloop()
The Function isn't executing in background but it executes first and then GUI is started.
Upvotes: 0
Views: 204
Reputation: 46669
You can use after()
to replace the while loop:
def alarm():
current_time = dt.datetime.now().time()
if current_time >= timetable[0]:
print("It's time")
else:
# check again 1 second later
root.after(1000, alarm)
clock()
alarm()
root.mainloop()
Upvotes: 2
Reputation:
You need threading
:
import threading
# Your Alarm definition
alarm_thread = threading.Thread(target=Alarm)
main_thread = threading.Thread(target=root.mainloop)
alarm_thread.start()
main_thread.start()
The functions will now run concurrently, but be aware that it will be tricky to manage interactions between the two. I don't know how the clock
function is defined or how you want it to run, but you can easily modify the code to make that one run concurrently too.
Upvotes: 1