tkinter-geometry-managers

Tkinter Geometry Managers

Python Tkinter Tutorial: Tkinter Geometry Managers

Tkinter Geometry Managers is a critical concept that is very important in understanding how the Tkinter widgets are placed on GUI applications.

Understanding Tkinter Geometry Managers

Geometry Management mainly deals with how the widgets are inserted in the GUI application. We have already seen the pack() method. Aside from pack(), there are two more methods: place() and grid(). We have already covered Tkinter Widgets in part-1. Using these two widgets, we shall proceed with understanding geometry management.

Keep in mind that without these methods(grid, place, pack) it is impossible to add widgets to GUI applications. And without a widget, there is no GUI application. Thus geometry managers are the crucial building block of GUI applications.

pack()

pack() directly inserts the widget on the root window. It flows in a sequential step, i.e., it is placed one after another. Moreover, the pack has three attributes: expand, fill, and side.

import tkinter as tk
root = tk.Tk()
root.geometry("250x250")
root.title("Pack")
btn_expand = tk.Button(text="Hinata",bg="yellow").pack(expand="yes")
btn_fill = tk.Button(text="Kageyama",bg="yellow").pack(fill="x")
btn_side = tk.Button(text="Tsukishima",bg="yellow").pack(side="left")
root.mainloop()
tkinter gemetry managers_pack

In the above code, we used three different arguments that come along with pack().

Expand: when set to True, the specific widgets expand throughout the root window. Fill is used to fill the widget in a complete GUI application based on the scale/dimension specified. NONE (default), X (fill only horizontally), Y (fill only vertically), or BOTH (fill both horizontally and vertically). Side is an argument that decides where the widget should be placed. The four-direction supported are TOP, BOTTOM, LEFT, and RIGHT.

grid()

grid() treats application in terms of rows and columns.

import tkinter as tk
root = tk.Tk()
root.geometry("265x150")
root.title("Grid")

btn_expand = tk.Button(text="Hinata",bg="yellow").grid(row=0,column=1)
btn_fill = tk.Button(text="Kageyama",bg="yellow").grid(row=0,column=2)
btn_side = tk.Button(text="Tsukishima",bg="yellow").grid(row=0,column=3)

root.mainloop()
tkinter gemetry managers_grid

place()

place method is used to place the widgets anywhere within the GUI root window. place() takes in x and y co-ordinate to place the specific widgets.

import tkinter as tk

root = tk.Tk()
root.geometry("250x150")
root.title("Place")

btn_expand = tk.Button(text="Hinata",bg="yellow").place(x=10,y=10)
btn_fill = tk.Button(text="Kageyama",bg="yellow").place(x=25,y=50)
btn_side = tk.Button(text="Tsukishima",bg="yellow").place(x=40,y=90)

root.mainloop()

Width And Height Attribute

place() also supports resizing and modifying the current Widget.

import tkinter as tk

root = tk.Tk()
root.geometry("280x350")
root.title("Place")

btn_expand = tk.Button(text="Hinata",bg="yellow").place(x=10,y=10,width=150,height=100)
btn_fill = tk.Button(text="Kageyama",bg="yellow").place(x=45,y=110,width=150,height=100)
btn_side = tk.Button(text="Tsukishima",bg="yellow").place(x=90,y=210,width=150,height=100)

root.mainloop()
place

The main advantage of adding width and height attributes is to change the dimensions of the widget based on the application requirement.

Design A Login EntryBox Using Grid And Place

First, let us create an import Tkinter and create a root window. One done using Label and EntryBox create a Login Box GUI application.

We shall first use the grid Tkinter geometry manager and then proceed with the place.

import tkinter as tk

root = tk.Tk()
root.geometry("280x350")
root.title("Place")

nameStr = tk.StringVar()
passwordStr = tk.StringVar()

nameLabel = tk.Label(text="Enter Name: ").grid(row=1,column=1)
passwordLabel = tk.Label(text="Enter Password: ").grid(row=2,column=1)
nameEntry = tk.Entry(textvariable=nameStr).grid(row=1,column=2)
passwordEntry = tk.Entry(textvariable=passwordStr).grid(row=2,column=2)

root.mainloop()

Last we shall see how to create a Login form box using the place method.

nameStr = tk.StringVar()
passwordStr = tk.StringVar()

nameLabel = tk.Label(text="Enter Name: ").place(x=50,y=100,width=130,heigh=45)
passwordLabel = tk.Label(text="Enter Password: ").place(x=50,y=200,width=130,heigh=45)
nameEntry = tk.Entry(textvariable=nameStr).place(x=170,y=100,width=100,heigh=45)
passwordEntry = tk.Entry(textvariable=passwordStr).place(x=170,y=200,width=130,heigh=45)

Subscribe to our Newsletter to never miss out on the content: https://animevyuh.org/newsletter. Join our Newsletter now for amazing Anime recommendations, Python, and Machine Learning Content.

Support Us: https://www.buymeacoffee.com/trjtarunhttps://ko-fi.com/tarunrjain751.
GitHub: https://github.com/lucifertrj.
Twitter: https://twitter.com/TRJ_0751.