I want my messagebox to pop-up in the foreground

Viewed 429

I want my messagebox to pop-up in the foreground over any application I'm running. Currently it pops-up in the background. I can tell only with the notification sound. I tried the toplevel, but I did not work. I'm running python 3.8

import time
from tkinter import *
from tkinter import messagebox


print("Testing program")
time.sleep(5)
print("Program tested")
window=Tk()
window.withdraw()
messagebox.showinfo('Successful','Script executed successfully')
window.deiconify()
window.destroy()
window.quit()
2 Answers

If you .destroy() the window, then you are closing it. Also, you need to use .mainloop(), and if you .quit() it, then it won't appear at all.

So here is your code. You need to use .attributes() and put it on the very top.

Code:

import time
from tkinter import *
from tkinter import messagebox


print("Testing program")
time.sleep(5)
print("Program tested")
window=Tk()
window.withdraw()
messagebox.showinfo('Successful','Script executed successfully')
window.deiconify()
window.attributes("-topmost", True)
window.mainloop()

Here's the working code for anyone who needs:

import time
from tkinter import *
from tkinter import messagebox


print("Testing program")
time.sleep(5)
print("Program tested")
window=Tk()
window.attributes('-topmost',1)
window.withdraw()
messagebox.showinfo('Successful','Script executed successfully')
window.deiconify()
window.attributes("-topmost", True)
window.destroy()
window.quit()
Related