I have a continuously running program that periodically reads an sqlite database. It calls a function to display messages in a tkinter window by checking certain criteria, and then it goes to sleep. My problem is that the window freezes when I try to close it (using the quit()
method). It could be that, in this case, root.mainloop()
is not the last statement of the program. (I need to display a pop-up window at regular intervals).
I believe multithreading is not a solution because tkinter should not be run in a separate thread.
What is the right approach to this problem?
Following is the code -
def display_reminders(task_names):
root = Tk()
root.title('Reminder')
root.geometry('-0-40')
root.minsize(width=300,height=100)
root.attributes("-topmost", True)
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
for key, name, dt in task_names:
Label(root, text=name).grid(padx=20, pady=20, sticky=NW)
btn = Button(root, text='Ok', command=root.quit)
btn.grid(pady=5)
btn.bind('<Return>', lambda e: root.quit())
root.mainloop()
def check_for_reminders(missed_flag=False):
# logic to read database and check if reminder should be displayed
display_reminders(task_names)
check_for_reminders(True)
time.sleep(60 - int(datetime.datetime.now().strftime('%S')))
while True:
check_for_reminders()
# We subtract the number of seconds from 60 because having only
# time.sleep(60) results in the number of seconds past the minute creeping
# up.
time.sleep(60 - int(datetime.datetime.now().strftime('%S')))