Python Forum
Tkinter messagebox and using datetime/timedelta
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter messagebox and using datetime/timedelta
Hi all

I made this really simple program for myself to remind me to take some breaks.

It works great and all, although there is one behavior that I did not expect and although I don't mind it so much, i'd like to understand why it happens.

When the while loop is going and an interval variable is passed, the messagebox will come up. However, until I close the messagebox, it won't repeatedly come up multiple times. In fact I believe even the 'if' statement does not come into play again until I close that message box. My questions are:
  • Shouldn't multiple messageboxes be coming up based on the if statement? Wouldn't the loop constantly be going, meaning multiple boxes come up?
  • Does everything stop (including the loop) when the messagebox comes up?

import datetime as dt
from tkinter import *

def setInterval():
    interval = dt.timedelta(minutes = int(entry_1.get()))
    StartTime =
    while True:
        if == StartTime + interval:
            messagebox.showinfo('STRETCH TIME',message = "Please take a stretch break!") 
            StartTime =

if  __name__ == "__main__":
    root = Tk()
    intervariable = IntVar()
    theLabel = Label(root,text="Enter time below in minutes (e.g 30)")
    entry_1 = Entry(root)
    button_1 = Button(root,text="Set interval",command=setInterval)
Appreciate any assistance :)
There are seven forms of message box. Each of these request some sort of input from the user before continuing with the current process.

I think it would be possible to create multiple messages if each were issued in a separate thread (ended when a response was received). This could, however cause some issues if allowed to run for an extended length of time without response to any of the messages. Perhaps a limit on how many are issued, which would require a notification from each thread when completed.
The problem you are having is the while loop in "setInterval()" prevents "root.mainloop()" from running. If root.mainloop() doesn't run you program does not react to events like button presses. mainloop() can be the only forever loop in you tkinter app.
Example of using the after method to do something after every second without blocking the GUI event loop.
import tkinter as tk
from tkinter import messagebox
import datetime as dt

class MainFrame(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        theLabel = tk.Label(self, text="Enter time below in minutes (e.g 30)")
        self.entry_1 = tk.Entry(self)
        button_1 = tk.Button(self, text="Set interval",
        self.timenow_label = tk.Label(self)
        self.alarm_time_label = tk.Label(self, text='Alarm time:')
        self.alarm_time = None

    def set_alarm_time(self):
            interval = dt.timedelta(minutes=int(self.entry_1.get()))
        except ValueError:
            self.entry_1.delete(0, tk.END)
                'Entry error', message="Please enter an integer")

        self.alarm_time = + interval
        self.alarm_time_label['text'] = self.alarm_time.strftime(
            'Alarm time: %H:%M:%S')

    def timer(self):
        timenow =
        self.timenow_label['text'] = timenow.strftime('Time now: %H:%M:%S')
        if self.alarm_time and timenow >= self.alarm_time:
            self.alarm_time = None
            self.alarm_time_label['text'] = 'Alarm time:'
            self.entry_1.delete(0, tk.END)
                'STRETCH TIME', message="Please take a stretch break!")

        self.after(1000, self.timer)

if __name__ == "__main__":
    app = tk.Tk()
    main_frame = MainFrame()
Larz60+ likes this post

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Sound from the tkinter.messagebox tedarencn 2 1,555 Jul-11-2020, 10:45 AM
Last Post: steve_shambles
  [Tkinter] Messagebox with playsound khandelwalbhanu 6 940 May-16-2020, 11:40 AM
Last Post: chesschaser
  [Tkinter] messagebox is not being executed please help erwinsiuda 2 552 Apr-02-2020, 01:56 AM
Last Post: Larz60+
  [Tkinter] passing data to messagebox kmusawi 0 725 Feb-18-2019, 01:51 AM
Last Post: kmusawi
  Adding timer on the Messagebox aniyanetworks 6 4,995 Feb-13-2019, 07:48 PM
Last Post: aniyanetworks
  [Tkinter] Button widget gets stuck from using tkinter.messagebox? Nwb 2 1,929 Jun-20-2018, 02:21 PM
Last Post: Nwb

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020