What is a better Tkinter geometry manager than .grid()?

TkinterPythonGUI-Programming

The Geometry Manager is one of the specific features in the Tkinter Library. It provides structure to all the Tkinter widgets in the window. The Geometry Manager is used for formatting the layout and position of the widget in a Tkinter application window.

To format the look and appearance of any widget, we have three general methods in Geometry Manager.

  • Pack Geometry Manager
  • Grid Geometry Manager
  • Place Geometry Manager

Each Geometry Manager has some features that provide a different style and layout to the widgets. The Pack Geometry Manager is the most commonly used layout manager which gives access to add padding, margin, fill and expand like properties of the widget in the canvas. Pack Manager is the simplest geometry manager for any Tkinter application.

Example

# Import the required library
from tkinter import *
from tkinter import ttk

# Create an instance of Tkinter window
win= Tk()

# Set the size of the window
win.geometry("700x350")

# Create a Button widget
ttk.Button(win, text= "Button").pack(padx= 20,pady=20, expand= 1)
win.mainloop()

Output

Grid Geometry Manager

The Grid Geometry Manager is useful for many complex applications where we have lots of widgets. It works on the basis of a coordinate geometry system. It places all the widgets in a grid such as rows and columns. You can provide a layout to any widget in the application by using Grid Manager.

Example

# Import the required library
from tkinter import *
from tkinter import ttk

# Create an instance of Tkinter window
win= Tk()

# Set the size of the window
win.geometry("700x350")
# Create a Button widget
ttk.Button(win, text= "Button1").grid(row=0, column=0)
ttk.Button(win, text= "Button2").grid(row=0, column=1)

win.mainloop()

Output

raja
Published on 07-Jun-2021 10:52:21
Advertisements