Tkinter place() method

Tkinter has quite a few geometry managers including pack, grid and place is one of them. Geometry managers are simply used to position widgets on the Tkinter window and in a way handle the layout of that window.

While the pack() method is used for the directional positioning of widgets, the grid() method is used for indexed placement of widgets. But they cannot be used together i.e. A Tkinter window can either have a grid-based geometry or a pack-based geometry. But place is very different from either of the former two geometry managers. Moreover, it can also be used along with either pack() or grid() method.

Tkinter place() method

Place is the simplest geometry manager in Tkinter. It’s the unique nature of this method that gives us the liberty to set the position of a widget in absolute as well as relative terms inside the parent. Also, the position of a widget can be easily set inside another widget as well. To use the place geometry manager, Tkinter place() method is used.

Following is the syntax of the place() method:

widget.place(**options)

Following are the possible options for this method:

  • height : height of the widget (pixels).
  • width : width of the widget (pixels).
  • relheight : height of the widget as a fraction of the height of the parent widget. Value could be a float between 0.0 and 1.0.
  • relwidth : height of the widget as a fraction of the height of the parent widget. Value could be a float between 0.0 and 1.0.
  • x : horizontal offset(pixels).
  • y : vertical offset(pixels).
  • relx : horizontal offset as a fraction of the width of the parent widget. Value could be a float between 0.0 and 1.0.
  • rely : vertical offset as a fraction of the height of the parent widget. Value could be a float between 0.0 and 1.0.
  • bordermode : Values are INSIDE (the default, ignores the parent’s border) and OUTSIDE(opposite of INSIDE).
  • in_ : specifies the parent widget inside which this widget has to be positioned.
  • anchor : indicates where a widget is positioned relative to a reference point like: N,S,E,W,NW,NE,SW,SE.The default is NW (the upper left corner of the widget).

 

Example

Below is given the Python program:

from tkinter import *
root=Tk()
#relheigth demo
b1 = Button(root, text="Relheight=0.6")
b1.place(relheight=0.6)
#relwidth demo
b2= Button(root, text="Relwidth=0.2")
b2.place(relwidth=0.2)
#y demo
b3=Button(root, text="Y=321px")
b3.place(y=321)
#x demo
b4=Button(root, text="x=400px")
b4.place(x=400)
#height demo
b5 = Button(root, text="Height=100px")
b5.place(height=100, x=300, y=300)
#width demo
b6 = Button(root, text="Width=150px")
b6.place(width=50, x=200, y=200)
#relx demo
b7=Button(root, text="Relx=0.3")
b7.place(relx=0.3)
#rely and in_ demo
b8=Button(root, text="Rely=0.7")
b8.place(in_=b5,rely=0.7)
root.mainloop()

To learn more about Tkinter:

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *