Skip to content
Related Articles

Related Articles

Improve Article

How to create a frame inside a frame tkinter?

  • Last Updated : 21 Jun, 2021

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:

 

 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :