In this video I’ll show you how to center a widget using the place method with Tkinter and Python.

Place() is similar to pack() and grid() but allows you to place a widget in a specific absolute or relative position on your app using x and y coordinates.

In this video we’ll center a button using relx and rely, which are relative positions that will move and resize as the app moves and resizes.

Python Code: center_place.py
(Github Code)

from tkinter import *

root = Tk()
root.title('Codemy.com - Center A Thing With Place')
root.iconbitmap('c:/gui/codemy.ico')
root.geometry("500x500")

button_1 = Button(root, text="Button 1", font=("Helvetica", 32))
button_2 = Button(root, text="Button 2", font=("Helvetica", 32))

button_1.grid(column=0, row=0)
button_2.grid(column=1, row=0)

my_button = Button(root, text="Click Me", font=("Helvetica, 32"))
my_button.place(relx=0.5, rely=0.5, anchor=CENTER)
#my_button.place(x=100, y=50)




root.mainloop()

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of...