Using the Place Layout Manager in Tkinter
Laying out widgets with the Place layout manager

Tkinter Tutorial Getting started with TKinter

Heads up! You've already completed this tutorial.

In this tutorial we are going to focus on the third of the layout managers in Tkinter, place, to get you on your way to building GUI applications.

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

The Place Layout Manager

The place layout manager allows for you to have more absolute control about 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 most 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 height and width of widget relative to the size of the parent widget, values between 0.0 and 1.0
  • anchor -- where the widget is placed in the parent widget, specified by 'n', 's', 'e', 'w', or some combination of them. Default is 'center'

Create A Simple GUI With Place

Let's take a look at a simple example to learn how to lay out widgets using place. Below we create a program that asks the user a question and allows the user to select an option from a list, using a Listbox, before closing the window.

python
from tkinter import *

def get_selection():
    '''
    Get users selection and print to terminal.
    '''
    selection = lb_of_cities.curselection()  # function takes current selection from listbox
    print(lb_of_cities.get(selection))

root = Tk()
root.title("Place layout Example")
root.geometry("300x300+50+100")  # width x length + x + y

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

# create listbox to hold names
lb_of_cities = Listbox(root, selectmode=BROWSE, width = 24)  # width is equal to number of characters
lb_of_cities.place(x=40, y=65)
cities = ["Beijing", "Singapore", "Tokyo", "Dubai", "New York"]

# add items to listbox
for  c  in  cities:
    lb_of_cities.insert(END, c)

# set binding on item select in listbox
# when item of listbox is selected, call the function get_selection
lb_of_cities.bind("<<ListboxSelect>>", lambda event:  get_selection())

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

root.mainloop()

The code above will produce the following GUI:

Tkinter GUI window created using place layout manager Tkinter GUI window created using place layout manager

The GUI application itself is very simple consisting of a label, a listbox, and a button. The example above only shows how to use absolute positioning by setting the x and y values. In lines 15, 19 and 32, each widget is arranged in the window by specifying these values.

Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt5
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]

Summary

Today's post covers some of the fundamentals of using place for layout management in Tkinter to create a GUI application. While this method may be a bit more time-consuming to use, it is very useful when you want more control over the exact location of your widgets.

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

Using the Place Layout Manager in Tkinter was written by Joshua Willman .