Layout Manager (Geometry Manager) in Python

Layout managers are also called as geometry managers.

They are used for positioning,arranging and registering widgets on tkinter window.

Python provides three layout/ geometry managers.
1. pack()
2. grid()
3. place()

pack()

The pack method is used to organize widget in to cavity on parent window.
Syntax

w.pack(options)

There are three possible options of pack method : expand, fill and side

  • expand : It can have boolean values , True and False. If value is set to True then widget expands to fill any space.If value is set to False then it is used in widget’s parent
  • fill : It is used to fill extra spaces allocated to it by pack(). This fill option has dimensions like – NONE (default), X(fill horizontally), Y(fill vertically) or BOTH(fill horizontally and vertically)
  • side : It is used to specify the side to which widget must be packed in parent window – TOP(default), BOTTOM,LEFT or RIGHT

  • Pack method with internal and external padding

    • padx : This option is used for external padding in X direction / horizontal padding.
    • pady : This option is used for external padding in Y direction / vertical padding.
    • ipadx : This option is used for internal padding in X direction / horizontal padding.
    • ipady : This option is used for internal padding in Y direction / vertical padding.


    """
    Author : ITVoyagers (itvoyagers.in)
    
    Date :12th March 2020
    
    Description : Simple Program to show use of pack() and its options
    """
    from tkinter import *
    root=Tk()
    w=Label(root,text="ITVOYAGERS",bg="blue")
    w.pack()
    w=Label(root,text="itvoyagers.in",bg="pink")
    #fill=X makes size of widget is equal to the parent widget
    w.pack(fill=X)
    #external padding in x direction (horizontally)
    Label(root,text="itv",bg="blue").pack(fill=X,padx=10)
    w=Label(root,text="itv2",bg="green")
    #external padding in y direction (vertically)
    w.pack(fill=X,pady=10)
    w=Label(root,text="itv3",bg="yellow")
    #internal padding in x direction (horizontally)
    w.pack(fill=X,ipadx=10)
    w=Label(root,text="itv4",bg="violet")
    #external padding in y direction (vertically)
    w.pack(fill=X,ipady=10)
    #use of side option
    w=Label(root,text="itv5",bg="red",fg="white")
    w.pack(padx=5,pady=10,side=LEFT)
    w=Label(root,text="itv6",bg="green",fg="black")
    w.pack(padx=5,pady=20,side=LEFT)
    w=Label(root,text="itv7",bg="blue",fg="white")
    w.pack(padx=5,pady=20,side=LEFT)
    

    grid()

    The grid method is used to organize widget in to grid / table like structure.
    Syntax

    w.grid(options)
    

    There are various possible options of grid method : column, columnspan, row, rowspan, padx, pady, ipadx, ipady and sticky

    • Column : It is the column number where you want to organize widget.The default value is zero.
    • Columnspan : It specifies number of columns a widget occupies. By default a widget occupies single cell of column but by using columnspan option you can grab multiple cells of a row and merge them into one larger cell.
    • Row : It is the row number where you wnat to organize the widget. the default value is zero.
    • Rowspan : It specifies number of rows a widget occupies. By default a widget occupies single cell of row but by using rowspan option you can grab multiple cells of a column and merge them into one larger cell.
    • padx & pady ( External padding) : padx option is used for external padding in X direction / horizontal padding & pady option is used for external padding in Y direction / vertical padding.
    • ipadx & ipady ( Internal padding) : ipadx option is used for internal padding in X direction / horizontal padding & ipady option is used for internal padding in Y direction / vertical padding.
    • Sticky : It is used in the condition where cell is larger than widget.If sticky is not used than by default the widget is placed in the center of the cell. Sticky can have values like N,E,S,W,NE,NW,SE & SW.
      i. If you set sticky option to NE, the position of widget is top right.
      ii. If you set sticky option to SE, the position of widget is bottom right.
      iii. If you set sticky option to NW, the position of widget is top left.
      iv. If you set sticky option to SW, the position of widget is bottom left.
    • """
      Author : ITVoyagers (itvoyagers.in)
      
      Date :12th March 2020
      
      Description : Simple Program to show use of grid() and its options
      """
      from tkinter import *
      root = Tk()
      b_col_sticky = Button(root, text="I'm in row 0 column 3 and staying in NW")
      b_col_sticky.grid(column=3, sticky = NW)
      
      b_columnspan = Button(root, text="I have a Columnspan of 3")
      b_columnspan.grid(columnspan=3)
      
      b_ipadx = Button(root, text="i have horizontal internal padding (ipadx) of 4")
      b_ipadx.grid(ipadx=4)
      
      b_ipady = Button(root, text="i have vertical internal padding (ipady) of 4")
      b_ipady.grid(ipady=4)
      
      b_padx = Button(root, text="i have horizontal external padding (padx) of 4")
      b_padx.grid(padx=4)
      
      b_pady = Button(root, text="i have vertical external padding (padx) of 4")
      b_pady.grid(pady=4)
      
      b_row = Button(root, text="I'm in row 2 column 0")
      b_row.grid(row=2)
      
      b_rowspan = Button(root, text="I have a Rowspan of 2")
      b_rowspan.grid(rowspan=2)
      
      

      place()

      The place method is used to organize widget in to parent window placing them in specific position.
      In absolute positioning we specify the position and size of each widget in pixels. The size and position of widget do not change if we resize the window
      Syntax

      w.place(options)
      

      There are various possible options of grid method : anchor, bordermode, height, width, relheight, relwidth, relx, rely, x, y

Leave a Comment