Tkinter

How to Change Background Color of the Window in Tkinter Python

In this tutorial, we are going to see how to change the background color of the window in Tkinter Python. The default background color of a Tkinter GUI is gray. You can change this to any color according to the needs of your application.

There are two ways to change the background color of a window in Tkinter:

  • By using the configure(bg=”) method of the tkinter.Tk class.
  • Set the bg property of tkinter.Tk directly.

In both cases, set the bg property with a valid color value. You can provide a valid color name or a 6-digit hexadecimal value with # preceding the value, as a string.
 

 

Example 1: Change Background Color of the Window Using Configure

In the following example we will change the background color of the Tkinter window to yellow.

from tkinter import *   

gui = Tk()  
gui.geometry('200x200')  

#set the window color
gui.configure(bg='yellow') 
  
gui.mainloop()

Output:


 
 

Example 2: Change Background Color of the Window Using bg Property

In the following example we will change the background color of the Tkinter window to yellow.

from tkinter import *   

gui = Tk()  
gui.geometry('200x200')  

#set the window color
gui['bg']='yellow'
  
gui.mainloop()

Output:


 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *