Tkinter

pack() Method in Tkinter Python 3

In this tutorial, we are going to see how to use pack() Method in Tkinter Python 3. This geometry manager organizes widgets into blocks before placing them in the parent widget.
 

Syntax:
widget.pack(options)

 

 
Here is the list of possible options:

  • expand: When set to true, the widget expands to fill any space not otherwise used in the parent widget.
  • fill: Determines whether the widget fills any extra space allocated to it by the parent, or keeps its own minimum dimensions: NONE (default), X (fill horizontally), Y (fill vertically) or BOTH (fill both horizontally and vertically).
  • side: Determines the side of the parent widget: TOP (default), BOTTOM, LEFT or RIGHT.

 

Example: pack() Method in Tkinter Python 3
from tkinter import *

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

btn1 = Frame(gui)
btn1.pack(side = BOTTOM)

btn2 = Button(frame, text="Button 1", bg="red", fg="white")
btn2.pack(side = LEFT)

btn3 = Button(frame, text="Button 2", bg="green", fg="white")
btn3.pack(side = LEFT)

btn4 = Button(frame, text="Button 3", bg="blue", fg="white")
btn4.pack(side = LEFT)

btn5 = Button(btn1, text="Button 4", bg="black", fg="white")
btn5.pack(side = BOTTOM)

gui.mainloop()

 

 
Output:

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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