Tkinter Place

In this post, I will teach you about the Tkinter place geometry manager. I will also be discussing various options that we can use with the help of code examples. So let’s get started.

Tkinter place geometry manager permits the developers to explicitly set the position of all the widgets inside the GUI window.you can either set the absolute or relative position of the widgets.

Despite its simplicity, I don’t recommend to use place geometry manager for simple layouts that’s because of too much work is required to get things done. I rather prefer the grid or pack geometry managers over it.

Let’s look at the options that we can use with place geometry manager.

Options :

anchorSet where the widget should be anchored to. use N, E, S, W, NE, NW(default), SE, or SW.
bordermodeSet the border style of the widget. use inside(default), outside.
heightSet the height of the widget in pixels.
widthSet the width of the widget in pixels.
relheightSet the relative height of the widget to the parent window as the float number between 0.0 and 1.0.
relwidthSet the relative width of the widget to the parent window as the float number between 0.0 and 1.0.
relxSet the relative horizontal position of the widget as a fraction to the width of parent window.default is 0.
relySet the relative vertical position of the widget as a fraction to the height of parent window.default is 0.
xSet the horizontal position of the widget. default is 0.
ySet the vertical position of the widget. default is 0.

Tkinter Place Examples :

For a better understanding of the concepts let’s look at some code examples.

Example 1

In this code example, we will use the x,y options of place geometry manager to set the position of two buttons.

from tkinter import *

window = Tk()

b1 = Button(window,text = "x : 0 , y : 0")
b1.place(x =0 ,y = 0)

b2 = Button(window,text = "x : 50 , y : 50")
b2.place(x =50 ,y = 50)

window.mainloop()

Output :

tkinter place code example 1 output

Example 2

In the below code, we will use the relx,rely options of place geometry manager to set the relative position of three buttons.

from tkinter import *

window = Tk()

b1 = Button(window,text = "relx : 0 , rely : 0")
b1.place(relx =0 ,rely = 0)


b2 = Button(window,text = "relx : 0.5 , rely : 0.5")
b2.place(relx =0.5 ,rely = 0.5)

b3 = Button(window,text = "relx : 0.3 , rely : 0.2")
b3.place(relx =0.3 ,rely = 0.2)

window.mainloop()

Output :

tkinter place code example 2 output

Example 3

In the following code example, We will set the height and width of the widgets.

from tkinter import *

window = Tk()

b1 = Button(window,text = "h : 20 , w : 10")
b1.place(x =0 ,y = 0,height = 100,width = 110)


b2 = Button(window,text = "h : 10 , w : 10")
b2.place(x =0,y = 100,height = 50,width = 100)

window.mainloop()

Output :

tkinter place code example 3 output

Example 4

In this final code example, we will set the relative height and width of the widgets.

from tkinter import *

window = Tk()

b1 = Button(window,text = "relh : 0.3 , relw : 0.6")
b1.place(x =0 ,y = 0,relheight = 0.3,relwidth = 0.6)


b2 = Button(window,text = "relh : 0.5")
b2.place(x =0,y = 100,relheight = 0.5)

window.mainloop()

Output :

tkinter place code example 4 output

That’s wrap for this tutorial.

Leave a Reply