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
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.
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
y-- specifies the specific x and y values of the widget in the parent window
rely-- horizontal and vertical offset relative to the size of the parent widget, values between 0.0 and 0.1
relheight-- set height and width of widget relative to the size of the parent widget, values between 0.0 and 0.1
anchor-- where the widget is placed in the parent widget, specified by
'w', or some combination of them. Default is
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.
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
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.
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.