How to place objects in the middle of a frame using tkinter?

To place objects in the middle of a frame, we can use the place method. Let's take an example and see how it's done.

Steps −

  • Import the required libraries and create an instance of tkinter frame.

  • Set the size of the frame using win.geometry method.

  • Next, create a button and label it.

  • Set the position of the buttons using the place method by supplying the x and y coordinate values.

  • Place the center of the widget at a relative x and y position of 0.5 of button widget (relx=0.5, rely=0.5). Set the anchor at the center by supplying "anchor=CENTER"

  • Finally, run the mainloop of the application window.


# Import the Tkinter library
from tkinter import *
from tkinter import ttk

# Create an instance of Tkinter frame
win = Tk()

# Define the geometry

# Create Buttons in the frame
button = ttk.Button(win, text="Button at the Center"), rely=0.5, anchor=CENTER)



When you execute this code, it will show the following output window −

Now, try resizing the window and you will notice that the button widget automatically centers itself accordingly.