How to change the frame background color in Tkinter

Tkinter is a python module that is used to create cross-platform GUI applications. This module is simple and lightweight to use. Tkinter provides us with a container called frame to group and organize widgets. In this answer, we will learn how to use python to change the background color of the frame in Tkinter.

Syntax

We can provide a color value to the parameter bg while calling the function Frame().

Frame(bg = "color")

Let's take a look at an example of this.

Example

#import tkinter module
import tkinter as tk

#create window
window = tk.Tk()

#provide size to window
window.geometry("300x300")

#create frame
fr = tk.Frame(window, bg= "orange")

#add text label to frame
tk.Label(fr, text="Hello From Educative !!!").pack(padx=40,pady=40)

fr.pack()

window.mainloop()

Explanation

  • Line 5: We create an instance of the Tkinter class Tk() and assign it to a variable window.

  • Line 8: We set the window size as 300x300.

  • Line 11: We create a frame using the Frame() function by specifying the background color as orange using the parameter bg.

  • Line 14: We create a text label and attach it to the frame fr.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved