Change the position of MessageBox - Tkinter - GeeksforGeeks

01 Oct, 2020
Prerequisite: 
Python tkinter
Python tkinter message box widget
Python tkinter toplevel widget 
Python offers many Libraries and Framework for Creating and developing GUI (Graphical User Interface). Out of all GUI libraries or frameworks, Tkinter is the most commonly used method because it is comparatively fast and easier when creating a GUI application.
MessageBox Widget is used to display the message boxes in a python application. For creating a message box, use Toplevel() widget. A Toplevel Widget is used to create a window on top of all other windows. It is used to provide some extra information to the user. These windows are directly organized and managed by the window manager and do not need to have any parent window associated with them every time.
Once your message box is ready, labels and buttons can be added to it to deliver specific information. 
Syntax:
Toplevel widget
toplevel = Toplevel(root, bg, fg, bd, height,....)

Create Label
Label(toplevel,text="Enter text here..").pack()

Create Button
Button(toplevel,text="Enter text here..").pack()

Set Size
toplevel=Toplevel(root)
toplevel.geometry("dimension")

Example: 
Python3
from tkinter import * def messagebox():    toplevel = Toplevel(root)     toplevel.title("QUIT")    toplevel.geometry("300x100")      l1=Label(toplevel, image="::tk::icons::question")    l1.grid(row=0, column=0, pady=(7, 0), padx=(10, 30), sticky="e")    l2=Label(toplevel,text="Are you sure you want to Quit")    l2.grid(row=0, column=1, columnspan=3, pady=(7, 10), sticky="w")     b1=Button(toplevel,text="Yes",command=root.destroy,width = 10)    b1.grid(row=1, column=1, padx=(2, 35), sticky="e")    b2=Button(toplevel,text="No",command=toplevel.destroy,width = 10)    b2.grid(row=1, column=2, padx=(2, 35), sticky="e")  root = Tk()root.geometry("300x200")root.title("Main Window")Button(root,text="Quit",command=messagebox,width = 7).pack(pady=80) root.mainloop()
Output:
Main Window(With Button)
Message Box(with Labels and Buttons)
 
Until now, there is no in-built method in tkinter to set the position of a message box. But position can be dealt with explicitly.
Syntax:
toplevel.geometry(f"dimension+{root.winfo_x()}+{root.winfo_y()}")

root.winfo_x()- Gives X current position of main window
root.winfo_y()- Gives Y current position of main window
OR
x_position = value
y_position = value

toplevel.geometry(f"dimension+{x_position}+{y_position}")

Example 1:
Python3
from tkinter import * def messagebox():    toplevel = Toplevel(root)     toplevel.title("QUIT")    toplevel.geometry(f"300x100+{root.winfo_x()}+{root.winfo_y()}")          l1=Label(toplevel, image="::tk::icons::question")    l1.grid(row=0, column=0, pady=(7, 0), padx=(10, 30), sticky="e")    l2=Label(toplevel,text="Are you sure you want to Quit")    l2.grid(row=0, column=1, columnspan=3, pady=(7, 10), sticky="w")     b1=Button(toplevel,text="Yes",command=root.destroy,width = 10)    b1.grid(row=1, column=1, padx=(2, 35), sticky="e")    b2=Button(toplevel,text="No",command=toplevel.destroy,width = 10)    b2.grid(row=1, column=2, padx=(2, 35), sticky="e")  root = Tk()root.geometry("300x200")root.title("Main Window")Button(root,text="Quit",command=messagebox,width = 7).pack(pady=80) root.mainloop()
Output:
Main Window
When you click on “Quit” button
Message Box
Example 2:
Python3
from tkinter import *  def messagebox():    toplevel = Toplevel(root)     toplevel.title("QUIT")    x_position = 300    y_position = 200    toplevel.geometry(f"300x100+{x_position}+{y_position}")     l1=Label(toplevel, image="::tk::icons::question")    l1.grid(row=0, column=0, pady=(7, 0), padx=(10, 30), sticky="e")    l2=Label(toplevel, text="Are you sure you want to Quit")    l2.grid(row=0, column=1, columnspan=3, pady=(7, 10), sticky="w")     b1=Button(toplevel, text="Yes", command=root.destroy, width=10)    b1.grid(row=1, column=1, padx=(2, 35), sticky="e")    b2=Button(toplevel, text="No", command=toplevel.destroy, width=10)    b2.grid(row=1, column=2, padx=(2, 35), sticky="e")  root = Tk()root.geometry("300x200")root.title("Main Window")Button(root, text="Quit", command=messagebox, width=7).pack(pady=80) root.mainloop()
Output:
When you click on “Quit” button It will display message box at position (300,200)
Picked
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
Python OOPs Concepts
Python | Get unique values from a list
Check if element exists in list in Python
Python Classes and Objects
Python | os.path.join() method
How To Convert Python Dictionary To JSON?
Python | Pandas dataframe.groupby()
Create a directory in Python