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 my 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_ – specify the master window for the widget.
- x, y – specify 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 0.1.
- relwidth, 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 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.
# import necessary modules
from tkinter import *
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)
# create listbox to hold names
lb_of_cities = Listbox(root, selectmode=BROWSE, width = 24) # width is equal to number of characters
cities = ["Beijing", "Singapore", "Tokyo", "Dubai", "New York"]
# add items to listbox
for c in cities:
# 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)
Get users selection and print to terminal.
selection = lb_of_cities.curselection() # function takes current selection from listbox
The code above will produce the following GUI:
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 10, 14 and 28, 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.
In following posts, we’ll be getting into using some of Tkinter’s other widgets. As always, if you have any questions of comments I would really enjoy hearing them!