Open In App

Change the position of MessageBox – Tkinter

Last Updated : 01 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: 

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.

Creating A Message Box

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)

 

Setting Position 

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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads