I am trying to create two kinds of popups using tinter with Python3. First is a simple wait till ok button pressed message. And another which runs a process in background and shows the popup till the process completes. I have the following code for the same:
from tkinter import *
import time
def open_window():
popup = Toplevel()
ok_button = Button(popup, text="OK", command=popup.destroy)
ok_button.pack()
popup.mainloop()
def open_event_window(message, on_event):
NORM_FONT = ("Helvetica", 15)
popup = Toplevel()
label = Label(popup, text=message, font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
popup.after(1, lambda: popup.focus_force())
popup.wm_attributes("-topmost", 1)
popup.focus_force()
popup.update()
try:
on_event()
except:
raise
finally:
try:
popup.destroy()
except:
pass
def func():
print(">>> ", 1)
time.sleep(5)
print(">>> ", 2)
root = Tk()
root.withdraw()
open_window()
time.sleep(5)
open_event_window("here", func)
print("-111111")
time.sleep(5)
With the above code the ok prompt works but the callback prompt doesn't show up. If I try to use Tk() directly instead of TopLevel() both the prompts work but then the first prompt only closes when the second prompt opens which is not what is desired. For the given code the flow I want is: open ok prompt and close on ok pressed -> sleep 5 sec -> open callback prompt and run for 5 seconds and close -> sleep 5 seconds -> exit.
Any guidance will be greatly appreciated. Thanks.
Note: the code with Tk() call in every function instead of TopLevel works properly with Python2 but I am trying to use Python3 where this happens.