Using the Place Geometry Manager in Tkinter

Laying out widgets with the Place geometry manager

Tkinter Tutorial Getting started with TKinter

Heads up! You've already completed this tutorial.

In this tutorial, we will focus on Tkinter's place geometry managers to help you build GUI applications.

If you are curious about the other geometry managers, or not sure which one to use for your project, you can check out the other tutorials:

The place Geometry Manager

The place geometry manager allows you to have more absolute control over the arrangement of your widgets. With place, you can specify the size of the widget, as well as the exact x and y coordinates to arrange it within the parent window. In many cases, this can prove to be easier when you are thinking about the layout of your GUI. But it also means that you may have to spend a little more time playing around with the x and y values.

The place manager is useful for arranging buttons or other smaller widgets together within a larger dialog window.

A few of the parameters you can play around with are listed below:

  • in_: specifies the master window for the widget
  • x, y: specifies the specific x and y values of the widget in the parent window
  • relx, rely: horizontal and vertical offset relative to the size of the parent widget, values between 0.0 and 1.0
  • relwidth, relheight: set the height and width of widgets relative to the size of the parent widget, values between 0.0 and 1.0
  • anchor: difines where the widget is placed in the parent widget, specified by 'n', 's', 'e', 'w', or some combination of them. The default is 'center'

A Demo GUI With place

Let's take a look at a quick example that shows how to lay out widgets on a dialog using the place geometry manager. Below, we create an app that asks the user a question and allows them to select an option from a Listbox:

python
import tkinter as tk

root = tk.Tk()
root.title("Place layout Example")
root.geometry("300x300+50+100")

def display_selection(event):
    selection = cities_listbox.curselection()
    print(cities_listbox.get(selection))

# Label to display the question
tk.Label(
    root,
    text="Which of the following cities would you like to travel to?",
    wraplength=200,
).place(x=50, y=20)

# Listbox to display the cities
cities_listbox = tk.Listbox(root, selectmode=tk.BROWSE, width=24)
cities_listbox.place(x=40, y=65)
cities = ["Beijing", "Singapore", "Tokyo", "Dubai", "New York"]
for city in cities:
    cities_listbox.insert(tk.END, city)

# Bind the listbox's selection
cities_listbox.bind("<<ListboxSelect>>", display_selection)

# Button to close the app
end_button = tk.Button(root, text="End", command=quit)
end_button.place(x=125, y=250)

root.mainloop()

The code above will produce the following GUI:

Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PyQt6 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book

Tkinter window using the place geometry manager Tkinter GUI window using place geometry manager

The GUI application itself is very minimal, consisting of a label, a listbox, and a button. The example above shows how to use absolute positioning by using the x and y arguments with the place geometry manager.

Summary

In this article, you learned the fundamentals of using place for geometry management in a Tkinter-based GUI application. While this geometry manager may be a bit more time-consuming to use, it is useful when you want more control over the exact position of your widgets on a given window.

Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PySide6 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Using the Place Geometry Manager in Tkinter was written by Joshua Willman with contributions from Leo Well .