Python tkinter place ( layout )

Positioning of widgets based on absolute and relative coordinates with respect to parent window.
Tkinter layout management using place for absolute & relative height width and positioning of widget

x and y of position of widget Here is an example of layout of the page to place the components using x , y coordinates.

We will keep Labels showing x ( horizontal ) and y ( vertical ) positions

Here x=0 is left edge and y=0 is top edge of the window.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x150")  # Size of the window 





my_w.mainloop()  # Keep the window open

relx & rely

relx and rely of widget We can use relative x ( horizontal ) and y ( vertical ) position with respect to width and height of the window by using relx and rely options.
relx and rely can take values from 0 to 1
relx=0 is left edge of the window relx=1 is right edge of the window.

rely=0 is top of the window and rely=1 is the bottom edge of the window.
Here default value for anchor='nw' is used.
l1=tk.Label(my_w,text='relx=.1,rely=.1 ',bg='yellow'),rely=.1)




Understanding anchor

anchor values
Four corners of widget as anchor The option anchor says which side or the corner of the widget to be considered for positing the widget in layout.
Values of anchor option is here


Using relx & rely with anchor at four corners

Placing widget at Four corners We will place four Labels at four corners of a window. We will use relx and rely with values either 0 or 1 to assign relative position to widgets.

We have to change the anchor values to position the widget at corners. Note that we have not used any fraction values for relx or rely.
l1=tk.Label(my_w,text='x=0, y=0, nw',bg='yellow'),rely=0,anchor='nw') # top left  

l2=tk.Label(my_w,text='x=1, y=0, ne',bg='yellow'),rely=0,anchor='ne') # top right 

l3=tk.Label(my_w,text='x=1, y=1, se',bg='yellow'),rely=1,anchor='se') # bottom right 

l4=tk.Label(my_w,text='x=0, y=1, sw',bg='yellow'),rely=1,anchor='sw') # bottom left 

anchor and x,y coordinates

Same x y coordinates Here both the buttons has same x and y coordinate value but are placed at different positions due to their respective anchor values. Note that the bottom right ( anchor='se' ) corner of first widget is touching the top left ( anchor='nw' ) of second widget.
b1=tk.Button(my_w,text='x=100,y=50,se'),y=50,anchor='se') # top left  


relwidth & relheight

relwidth & relheight of the widget The dimension of the widget can be fixed based on the size of the parent window by using relwidth and relheight options.

Here relwidth=1 will occupy the full width of the parent window, similarly relheight=1 will occupy the full height of the window.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x150")  # Size of the window 


my_w.mainloop()  # Keep the window open
Drag and Drop a button by using place layout
Pack Layout Grid Layout Frame rowconfigure() & columnconfigure()
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    Thank you

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer