How to center a label in a frame of fixed size in Tkinter?

Tkinter is Python's standard GUI toolkit for building desktop applications. The Frame widget acts as a container to organize other widgets, and you can customize its size, background, and layout using geometry managers.

To center a label inside a fixed-size frame, you can use the place() geometry manager with relx=0.5, rely=0.5, and anchor=CENTER properties.

Basic Centering Example

Here's how to center a label within a frame using the place() geometry manager ?

import tkinter as tk

# Create main window
root = tk.Tk()
root.geometry("700x350")
root.title("Centered Label in Frame")

# Create a fixed-size frame
frame = tk.Frame(root, width=300, height=200, bg="lightblue", relief="solid", bd=2)
frame.pack(pady=50)
frame.pack_propagate(False)  # Maintain fixed size

# Create and center the label inside the frame
label = tk.Label(frame, text="I am centered!", font=("Arial", 16, "bold"), bg="white")
label.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

root.mainloop()

Using Grid Geometry Manager

Alternatively, you can use the grid() manager with sticky parameter ?

import tkinter as tk

# Create main window
root = tk.Tk()
root.geometry("600x400")

# Create a fixed-size frame
frame = tk.Frame(root, width=400, height=250, bg="lightgreen", relief="raised", bd=3)
frame.pack(pady=30)
frame.grid_propagate(False)  # Maintain fixed size

# Configure grid to center content
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)

# Create label using grid
label = tk.Label(frame, text="Centered with Grid!", font=("Times", 18), bg="yellow")
label.grid(row=0, column=0)

root.mainloop()

Multiple Centered Labels

You can center multiple labels at different positions within the frame ?

import tkinter as tk

# Create main window
root = tk.Tk()
root.geometry("800x500")

# Create frame
frame = tk.Frame(root, width=500, height=300, bg="lightcoral", relief="groove", bd=4)
frame.pack(pady=50)
frame.place_slaves()  # Maintain fixed size

# Center label
center_label = tk.Label(frame, text="CENTER", font=("Arial", 14, "bold"))
center_label.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

# Top center label
top_label = tk.Label(frame, text="TOP CENTER", font=("Arial", 12))
top_label.place(relx=0.5, rely=0.2, anchor=tk.CENTER)

# Bottom center label
bottom_label = tk.Label(frame, text="BOTTOM CENTER", font=("Arial", 12))
bottom_label.place(relx=0.5, rely=0.8, anchor=tk.CENTER)

root.mainloop()

Key Parameters

Parameter Description Example Value
relx Horizontal position (0.0 to 1.0) 0.5 (center)
rely Vertical position (0.0 to 1.0) 0.5 (center)
anchor Widget anchor point tk.CENTER

Conclusion

Use place(relx=0.5, rely=0.5, anchor=CENTER) for precise centering in frames. The grid() method with weight configuration also works well for responsive centering.

Updated on: 2026-03-26T18:55:47+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements