Open In App

How to create a frame inside a frame tkinter?

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Tkinter

It is very easy to create a basic frame using Tkinter, this article focuses on how another frame can be created within it. To create a basic frame the name of the parent window is given as the first parameter to the frame() function. Therefore, to add another frame within this frame, just the name of the first frame needs to given to the second frame as the parent window.

Optional values such as frame padding are give relative to the parent window. We can add multiple frames in this fashion using the same approach, by making the previous frame the parent of the current frame.

Approach:

  • Create Normal Tkinter Window
  • Create first frame normally
  • Create second frame
  • Take the first window as its parent window
  • Execute code

frame() is an inbuilt Tkinter method which helps realize our required functionality.

Syntax: frame(master)

Parameter:

  • master: parent window
  • highlightcolor: To set the color of the focus highlight when widget has to be focused.
  • bd: to set the border width in pixels.
  • bg: to set the normal background color.
  • cursor: to set the cursor used.
  • width: to set the width of the widget.
  • height: to set the height of the widget.

Program:

Python3




# Import Module
from tkinter import *
 
# Create Tkinter Object
root = Tk()
 
# Set Geometry
root.geometry("400x400")
 
# Frame 1
frame1 = Frame(root,bg="black",width=500,height=300)
frame1.pack()
 
# Frame 2
frame2 = Frame(frame1,bg="white",width=100,height=100)
frame2.pack(pady=20,padx=20)
 
# Execute Tkinter
root.mainloop()


 

 

Output:

 

 


Last Updated : 21 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads