[Solved] Tkinter, try to keep using the program on while executing


You can use root.after(miliseconds, function_name, argument) to execute function with delay and root.mainloop() (and program) will work normally.

You can use datetime to convert text to datetime object, substract two dates and get difference in milliseconds.

try:
    import tkinter as tk
    import tkinter.messagebox as tkMessageBox
    print("Python 3")
except:
    import Tkinter as tk
    import tkMessageBox
    print("Python 2")

from datetime import datetime as dt

# --- functions ---

def callback():
    var_open = open_hour.get()
    var_close = close_hour.get()

    if not validateDate(var_open, var_close):
        tkMessageBox.showinfo("Error", "Incorrect times")
    else:
        # current date and time 
        current = dt.now()
        print('current:', current)

        # convert strings to `time` object
        time_open = dt.strptime(var_open, '%H:%M').time()
        time_close = dt.strptime(var_close, '%H:%M').time()

        print('time_open:', time_open)
        print('time_close:', time_close)

        # combine `current date` with `time`
        today_open = dt.combine(current, time_open)
        today_close = dt.combine(current, time_close)

        print('today_open:', today_open)
        print('today_close:', today_close)

        # substract dates and get milliseconds
        milliseconds_open = (today_open - current).seconds * 1000
        milliseconds_close = (today_close - current).seconds * 1000

        print('milliseconds_open:', milliseconds_open)
        print('milliseconds_close:', milliseconds_close)

        # run functions with delay
        root.after(milliseconds_open, show_open_message, var_open)
        root.after(milliseconds_close, show_close_message, var_close)

def validateDate(time1, time2):
    # TODO: check dates
    return True

def show_open_message(arg):
    tkMessageBox.showinfo("Info", "Open time: " + arg)

def show_close_message(arg):
    tkMessageBox.showinfo("Info", "Close time: " + arg)

# --- main ---

root = tk.Tk()

open_hour = tk.StringVar()
close_hour = tk.StringVar()

e1 = tk.Entry(root, textvariable=open_hour)
e1.pack()
e2 = tk.Entry(root, textvariable=close_hour)
e2.pack()

b = tk.Button(root, text="Start", command=callback)
b.pack()

root.mainloop()

solved Tkinter, try to keep using the program on while executing