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
- 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.
Pack method with internal and external 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
- anchor : It is used in the condition where cell is larger than widget.If anchor is not used than by default the widget is placed in the upper left corner ie NW. Anchor can have values like N,E,S,W,NE,NW,SE & SW.
- bordermode : INSIDE (the default) to indicate that other options refer to the parent’s inside (ignoring the parent’s border); OUTSIDE otherwise.
- height and width : It is used to specify height and width in pixels.
- relheight and relwidth : It is used to determine height and width as a float between 0.0 and 1.0, as a fraction of the height and width of the parent widget.
- relx and rely : It is used to determine horizontal and vertical offset as a float between 0.0 and 1.0, as a fraction of the height and width of the parent widget.
- x and y : It is used to determine horizontal and vertical offset in pixels.
""" Author : ITVoyagers (itvoyagers.in) Date :12th March 2020 Description : Simple program to show use of place() & its options """ from tkinter import * top = Tk() L1 = Label(top, text = "Python") L1.place(x = 10,y = 10) E1 = Entry(top, bd = 5) E1.place(x = 60,y = 10) L2 = Label(top,text = "Java") L2.place(x = 10,y = 50) E2 = Entry(top,bd = 5) E2.place(x = 60,y = 50) L3 = Label(top,text = "Marks") L3.place(x = 10,y = 150) E3 = Entry(top,bd = 5) E3.place(x = 60,y = 150) B = Button(top, text = "Sum") B.place(x = 100, y = 100)
You can also check GUI libraries, Advantages & Disadvantages of GUI
- GUI programming in Python and Python GUI Library
- What is tkinter?
- Events and Bindings in Python along with Widget configuration and styling
- Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
- Canvas widget of tkinter module – Python
- Widgets in tkinter module – Python
- Label, Text, Entry & Message Widget in Python
- Button, Checkbutton & Radiobutton Widget in Python
- Best post on Menu and Menubutton Widget of tkinter
- Best post- Listbox and Scrollbar(Slider) Widget-Python
- Best post on Frame Widget of tkinter in Python
- Best post on Spinbox and Scale Widget in Python
- Best post- LabelFrame, Toplevel, PanedWindow widgets
- Best post: message box widget and its methods
- Best post:Dimensions, Anchors, Bitmaps & Cursors in Python