How do I change the background of a Frame in Tkinter?

TkinterPythonServer Side ProgrammingProgramming

In order to change the background color and foreground color of a tkinter frame, we can assign different values to the bg and fg parameters in the Frame function.

Example

In this example, we have created two frames with different background colors.

#Import the required libraries
from tkinter import *

#Create an instance of tkinter frame
win= Tk()

#Set the geometry of frame
win.geometry("650x250")

#Create an frame
frame1= Frame(win, bg= "red")
frame2= Frame(win, bg="black")

#Create an label inside the frame
Label(frame2, text= "Line:1", font=('Lucida font',20)).pack(pady=20)
Label(frame1, text= "Line:2", font=('Lucida font',20)).pack(pady=20)

frame1.pack()
frame2.pack()

win.mainloop()

Output

Running the above code will display a window containing two frames with different background colors.

raja
Published on 27-Mar-2021 10:46:23
Advertisements