Python 3 – Tkinter’s pack method

Python 3 – Tkinter’s pack Method

This geometry manager organizes widgets into blocks before placing them into a parent widget.

Syntax

widget.pack( pack_options )

Below is a list of possible options −

  • expand − When set to true, the widget automatically expands to fill any unused space in its parent.
  • fill − Specifies whether the widget fills any additional space allocated by the packer, or maintains its minimum size: NONE (default, no fill), X (only horizontal fill), Y (only vertical fill), or BOTH (both horizontal and vertical fill).

  • side − Specifies the side of the parent widget to which the anchor should be attached: TOP (default), BOTTOM, LEFT, or RIGHT.

Example

Try the following examples and move the cursor over different buttons to see the effects −

# !/usr/bin/python3
from tkinter import *

root = Tk()
frame = Frame(root)
frame.pack()

bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )

redbutton = Button(frame, text = "Red", fg = "red")
redbutton.pack( side = LEFT)

greenbutton = Button(frame, text = "Brown", fg = "brown")
greenbutton.pack( side = LEFT )

bluebutton = Button(frame, text = "Blue", fg = "blue")
bluebutton.pack( side = LEFT )

blackbutton = Button(bottomframe, text = "Black", fg = "black")
blackbutton.pack( side = BOTTOM)

root.mainloop()

When the above code is executed, the following results will be produced −

Python 3 - Tkinter's pack method

Leave a Reply

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