back next

The tkinter 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 the Place Manager

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. Another use is to position control buttons in dialogs.

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 Button in the upper right corner of the frame. The button will overlap the label.

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

The following excerpt from a 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 vertically. 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 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)

Reference #

Place (class) [#]

Place geometry manager. This is an implementation class; all the methods described below are available on all widget classes.

place(**options) [#]

Place this widget relative to its parent.

**options
Geometry options.
anchor=
Default is NW.
bordermode=
Default is INSIDE.
height=
No default value.
in=
Default is ..
relheight=
No default value.
relwidth=
No default value.
relx=
Default is 0.
rely=
Default is 0.
width=
No default value.
x=
Default is 0.
y=
Default is 0.

place_configure(**options) [#]

Same as place.

place_forget() [#]

The place_forget method.

place_info() [#]

The place_info method.

place_slaves() [#]

The place_slaves method.

slaves() [#]

Same as place_slaves.

 

A Django site. rendered by a django application. hosted by webfaction.