Tkinter - Layout Managers

GUI (Graphical User Interface) is the common User Interface which is a visual representation of communication for the easy interaction between the machine and user. It includes buttons and icons for Graphical representation, and rather than the usual text-based or command-based communication it can be performed by interacting with these icons. Each and every GUI has its own features and functions.

Even without any prior computer knowledge one can perform the basic functions of a computer. Surprisingly, even for such a simple task GUI is responsible. GUI provides a visual representation of files present and provides details about it which makes searching very easy. Every response from the computer is visually communicated through GUI.

When designing the GUI of an application, it is important to decide what type of widgets are going to be used and how to organize those widgets in the application by using the specialized non-visible objects called layout managers in the Tkinter module.

Tkinter has three built-in layout managers:

Pack:

The pack geometry manager organizes widgets in horizontal and vertical boxes.

Code:

  # importing Tkinter module
  from tkinter import *
  
  # creating window object
  window = Tk()

  window.title("My First GUI Program")   # window title
  window.minsize(width=300, height=300)  # window size

  # creating Entry
  my_label = Label(text="I am a label", font=("Arial", 24, "bold"))
  my_label.pack()

  # creating Entry
  input = Entry(width=30)
  input.pack()

  # creating Button
  button = Button(text="Click Me")
  button.pack()

  window.mainloop()  
  
  Output:
                  

Place:

The place geometry manager positions widgets using absolute positioning.

Code:

  # importing Tkinter module
  from tkinter import *

  # creating window object
  window = Tk()

  window.title("My First GUI Program")   # window title
  window.minsize(width=300, height=300)  # window size

  # creating Label
  my_label = Label(text="I am a label", font=("Arial", 24, "bold"))
  my_label.place(x=0, y=0)               # position of Label

  # creating Entry
  input = Entry(width=30)
  input.place(x=100, y=100)              # position of Entry

  # creating Button
  button = Button(text="Click Me")
  button.place(x=200, y=200)             # position of Button

  window.mainloop()
  
  Output:
                  

Grid:

The grid geometry manager places widgets in a two dimensional grid.

Code:

  # importing Tkinter module
  from tkinter import *

  # creating window object
  window = Tk()

  window.title("My First GUI Program")   # window title
  window.minsize(width=300, height=300)  # window size

  # creating Label
  my_label = Label(text="I am a label", font=("Arial", 24, "bold"))
  my_label.grid(column=0, row=0)         # position of Label

  # creating Entry
  input = Entry(width=30)
  input.grid(column=1, row=1)            # position of Entry

  # creating Button
  button = Button(text="Click Me")
  button.grid(column=1, row=2)           # position of Button

  window.mainloop()
  
  Output:
                  

Among all these built-in layout managers, I prefer the Grid because it makes designing and collaborating the design with other designers easier as they provide a plan for where to place elements.

Om Jaiswal

Executive Committee Member at IEEE CIS SBC - GHRCE(62361B)

8 Comments

Sajid Hussain Reply

Simple, clean and elegant with a proper and sound introduction.

Om Jaiswal Reply

Thank you, it is really great to hear that.

Shantanu Lokhande Reply

Written with proper formatting and ample examples. Loved it!

Om Jaiswal Reply

Thank you, for your kind words.

Parag Asare Reply

Content could have been better and more that it is, but still great work.

Om Jaiswal Reply

I value and respect your opinion, thank you for sharing and I will work on it.

Rishank Tambe Reply

I was looking to read something about this for a long time. Thank you !

Om Jaiswal Reply

Thank you, I am happy to hear you feel that way!

Leave a Reply

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