Create Multiple frames with Grid manager using Tkinter (original) (raw)

Last Updated : 16 Mar, 2021

Prerequisites: Tkinter

Tkinter can support the creation of more than one widget in the same frame. Not just this it also supports a mechanism to align them relative to each other. One of the easiest ways of aligning the different widgets in the Tkinter is through grid manager. Apart from aligning various widgets, the grid manager can also be used for aligning the numerous frames.

In this article, we will be discussing the approach of aligning multiple frames with Grid Manager.

For this first the frames needs to be defined, and then they need to be aligned using grid().

Syntax:

frame1=LabelFrame(app, text="#Text you want to give in frame")

frame1.grid(row=#Row value, column=#Column value)

Function Used

Approach

Program:

Python `

Import the library tkinter

from tkinter import *

Create a GUI app

app = Tk()

Give a title to your app

app.title("Vinayak App")

Constructing the first frame, frame1

frame1 = LabelFrame(app, text="Fruit", bg="green", fg="white", padx=15, pady=15)

Displaying the frame1 in row 0 and column 0

frame1.grid(row=0, column=0)

Constructing the button b1 in frame1

b1 = Button(frame1, text="Apple")

Displaying the button b1

b1.pack()

Constructing the second frame, frame2

frame2 = LabelFrame(app, text="Vegetable", bg="yellow", padx=15, pady=15)

Displaying the frame2 in row 0 and column 1

frame2.grid(row=0, column=1)

Constructing the button in frame2

b2 = Button(frame2, text="Tomato")

Displaying the button b2

b2.pack()

Make the loop for displaying app

app.mainloop()

`

Output: