Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Change the position of MessageBox using Python Tkinter
When creating dialogue boxes in Python Tkinter, you often need to control where they appear on the screen. The Toplevel widget creates a new window that appears on top of the main window, and you can position it precisely using the geometry() method.
Understanding MessageBox Positioning
The Toplevel widget creates a separate window that appears above the main application window. To control its position, we use the geometry() method with the format: "widthxheight+x_position+y_position".
Example
Here's how to create a positioned message box using Tkinter ?
import tkinter as tk
from tkinter import *
def messagebox():
# Create MessageBox dialog that appears on top of the screen
top = Toplevel(win)
top.title("Click Me")
# Define the position of the MessageBox
x_position = 600
y_position = 400
top.geometry(f"600x200+{x_position}+{y_position}")
# Add content to the messageBox
label = Label(top, text="Hello! TutorialsPoint", bg="green", fg="white",
font=('Times New Roman', 24), height=5, width=30)
label.pack(pady=50)
# Create the main tkinter window
win = Tk()
win.geometry("600x200")
win.title("Window-1")
# Create button to trigger message box
Button(win, text="Click Me", command=messagebox, width=8).pack(pady=80)
win.mainloop()
How the Positioning Works
The geometry() method accepts a string in the format:
- Width x Height: Sets the size of the window (600x200 pixels)
- +X Position: Distance from left edge of screen (+600 pixels)
- +Y Position: Distance from top edge of screen (+400 pixels)
Customizing Position
You can position the message box anywhere on screen by changing the x and y coordinates ?
import tkinter as tk
from tkinter import *
def center_messagebox():
top = Toplevel(win)
top.title("Centered MessageBox")
# Center the message box on screen
screen_width = top.winfo_screenwidth()
screen_height = top.winfo_screenheight()
window_width = 400
window_height = 150
x = (screen_width // 2) - (window_width // 2)
y = (screen_height // 2) - (window_height // 2)
top.geometry(f"{window_width}x{window_height}+{x}+{y}")
Label(top, text="I'm centered!", font=('Arial', 16)).pack(pady=50)
# Create main window
win = Tk()
win.geometry("400x200")
win.title("Position Demo")
Button(win, text="Center MessageBox", command=center_messagebox).pack(pady=70)
win.mainloop()
Key Points
-
Toplevel()creates a new window on top of the main window -
geometry()controls both size and position of the window - Coordinates start from the top-left corner of the screen
- Use
winfo_screenwidth()andwinfo_screenheight()for dynamic positioning
Conclusion
Use the geometry() method with Toplevel to precisely position message boxes. The format "widthxheight+x+y" gives you complete control over window placement on screen.
