Tkinter Widget Overview
  Copyright 1997 by Fredrik Lundh <[email protected]>  
  Updated 9 Aug 1997  

The Place Geometry Manager

The Place geometry manager is the simplest of the three general geometry managers provided in Tkinter. It allows you explicitly set the position and size of a window, either in absolute terms, or relative to another window.

You can access the place manager through the place method which is available for all standard widgets.

When to use place

It is usually not a good idea to use place for ordinary window and dialog layouts; its simply to much work to get things working as they should. Use the pack or grid managers for such purposes.

However, place has its uses in more specialized cases. Most importantly, it can be used by compound widget containers to implement various custom geometry managers. For example, the Notepad and SplitPane widgets use the place geometry manager. Another use is to position control buttons in dialogs, like the pushpin button in the PropertySheet dialog.

Patterns

Let's look at some usage patterns. The following command centers a widget in its parent:

w.place(relx=0.5, rely=0.5, anchor=CENTER)

Here's another variant. It packs a Label widget in a frame widget, and then places a DrawnButton in the upper right corner of the frame. The button will overlap the label. To see the result, check out the screenshots from of the Visualizer application.

pane = Frame(master)
Label(pane, text="Pane Title").pack()
b = DrawnButton(pane, (12, 12), launch_icon, command=self.launch)
b.place(relx=1, x=-2, y=2, anchor=NE)

The following excerpt from the Notepad widget implementation displays a notepad page (implemented as a Frame) in the notepad body frame. It first loops over the available pages, calling place_forget for each one of them. Note that it's not an error to "unplace" a widget that it's not placed in the first case:

for w in self.__pages:
    w.place_forget()
self.__pages[index].place(in_=self.__body, x=bd, y=bd)

You can combine the absolute and relative options. In such cases, the relative option is applied first, and the absolute value is then added to that position. In the following example, the widget w is almost completely covers its parent, except for a 5 pixel border around the widget.

w.place(x=5, y=5, relwidth=1, relheight=1, width=-10, height=-10)

You can also place a widget outside another widget. For example, why not place two widgets on top of each other:

w2.place(in_=w1, relx=0.5, y=-2, anchor=S, bordermode="outside")

Note the use of relx and anchor options to center the widgets under each other. You could also use (relx=0, anchor=SW) to get left alignment, or (relx=1, anchor=SE) to get right alignment.

By the way, why not combine this way to use the packer with the launch button example shown earlier. The following example places two buttons in the upper right corner of the pane:

b1 = DrawnButton(pane, (12, 12), launch_icon, command=self.launch)
b1.place(relx=1, x=-2, y=2, anchor=NE)
b2 = DrawnButton(pane, (12, 12), info_icon, command=self.info)
b2.place(in_=b1, x=-2, anchor=NE, bordermode="outside")

Finally, let's look at a piece of code from an imaginary SplitWindow container widget. The following piece of code splits frame into two subframes called f1 and f2.

f1 = Frame(frame, bd=1, relief=SUNKEN)
f2 = Frame(frame, bd=1, relief=SUNKEN)
split = 0.5
f1.place(rely=0, relheight=split, relwidth=1)
f2.place(rely=split, relheight=1.0-split, relwidth=1)

To change the split point, set the split to something suitable, and call the place method again. If you haven't changed an option, you don't have to specify it again.

f1.place(relheight=split)
f2.place(rely=split, relheight=1.0-split)

You could add a small frame to use as a dragging handle, and add suitable bindings to it, e.g:

f3 = Frame(frame, bd=2, relief=RAISED, width=8, height=8)
f3.place(relx=0.9, rely=split, anchor=E)
f3.bind("<B1-Motion>", self.adjust)

Methods

place( option=value, ... ). Place the widget as described by the options (see below).

place_config(). Not in Tkinter 1.63. Same as place_configure.

place_configure( option=value, ... ). Not in Tkinter 1.63. Use place() instead. Place the widget as described by the options.

place_forget(). Remove the widget. The widget is not destroyed, and can be displayed again by place or any other manager.

place_info(). Return a dictionary the current options.

place_slaves(). Returns a list of the "slave" widgets managed by this widget. The widgets are returned as Tkinter widget references.

Options

The following options can be used with the place and place_configure methods:

Option Type Description
anchor constant Specifies which part of the widget that should be placed at the given position. Valid values are N, NE, E, SE, SW, W, NW, or CENTER. Default is NW (the upper left corner, that is).
bordermode string If "inside," the size and position are relative to the reference widget's inner size, excluding any border. If "outside", it's relative to the outer size, including the border. Default is "inside".
in or in_ widget Place widget relative to the given widget. You can only place a widget relative to its parent, or to any decendant of its parent. If this option is not given, it defaults to the parent. Note that in is a reserved word in Python. To use it as a keyword option, append an underscore (in_).
relwidth, relheight float Size, relative to the reference widget.
relx, rely float Position, relative to the reference widget (usually the parent, unless otherwise specified by the in option). 0.0 is the left (upper) edge, 1.0 is the right (lower) edge.
width, height integer Size, in pixels. If omitted, defaults to the widget's "natural" size.
x, y integer Position, in pixels. If omitted, defaults to 0.