Open tkinter messagebox that self-destructs in the lower right corner of the screen instead of the center

252 Views Asked by At

The messagebox by default opens in the center of the screen:

enter image description here

But I would like it to open at the bottom right of the screen:

enter image description here

My original code:

from tkinter import Tk
from tkinter.messagebox import Message 
from _tkinter import TclError

TIME_TO_WAIT = 1000
root = Tk()
root.withdraw()
try:
    root.after(TIME_TO_WAIT, root.destroy) 
    Message(title="Visual Studio Code", message="New Match Found", master=root).show()
except TclError:
    pass

As per indications, I tried using root.geometry but realized that it doesn't work for messagebox, only for standard box:

root = Tk()
x = 1000
y = -1000
root.geometry(f'250x150+{x}+{y}')
root.withdraw()

# rest of code...

Prints dedicated to Claudio's answer (to help understand our debate):

enter image description here

enter image description here

1

There are 1 best solutions below

9
Claudio On

The code below works on my system displaying the message box in the right bottom corner of the display. The 'trick' is that the messagebox hoovers over the parent window, so the appropriate setting of root.geometry() causes it to appear where the root window is placed on the screen. You have found it out by yourself already, but the root.withdraw() prevented the expected placement of the messagebox:

from tkinter import Tk
from tkinter.messagebox import Message 

TIME_TO_WAIT = 3000

root = Tk()
root.geometry(f'100x100+{root.winfo_screenwidth()-100}+{root.winfo_screenheight()-100}')
root.after(TIME_TO_WAIT, root.destroy)
Message(title="Visual Studio Code", message="New Match Found", master=root).show()