- Setting Background color for Tkinter in Python
- Example
- Output
- Tkinter Window Background Color
- Examples
- 1. Change background color using configure method
- 2. Change background color using bg property
- 3. Using background for bg
- 4. Using HEX code for background color
- Summary
- Set Tkinter Background Color
- Method configure(background= )
- Attribute bg or background
- Tkinter Color Code
- How to change Tkinter Button Background Color?
- Examples
- 1. Set Tkinter Button color to blue
- 2. Set Tkinter Button background color to black
- 3. Set Tkinter Button background color to Hex color
- Summary
Setting Background color for Tkinter in Python
We can customize the tkinter widgets using the tkinter.ttk module. Tkinter.ttk module is used for styling the tkinter widgets such as setting the background color, foreground color, activating the buttons, adding images to labels, justifying the height and width of widgets, etc.
In order to add a background color in tkinter widgets, we can specify the background property in the widget.
Example
In the following example, we will create a button that will change the background of the text label.
#Import the tkinter library from tkinter import * from tkinter.ttk import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("600x400") #Add a class to style the tkinter widgets style = ttk.Style() style.configure('TEntry', foreground = 'red') #Define a function to change the text color def change_color(): text.configure(background="red") #Create a text widget text=Label(win,text="This is a New Text",foreground="black", background="yellow",font=('Aerial bold',20)) text.pack(pady=20) #Create a Button widget Button(win, text= "Click Here", command= change_color).pack(pady=10) win.mainloop()
Output
Running the above code will create a window containing a text label with a «yellow» background color.
Now, click the «Click Here» button. It will change the background color of the text label to «Red».
Tkinter Window Background Color
The default background color of a GUI with Tkinter is grey. You can change that to any color based on your application’s requirement.
In this tutorial, we will learn how to change the background color of Tkinter window.
There are two ways through which you can change the background color of window in Tkinter. They are:
- using configure(bg=») method of tkinter.Tk class. or
- directly set the property bg of tkinter.Tk.
In both of the above said cases, set bg property with a valid color value. You can either provide a valid color name or a 6-digit hex value with # preceding the value, as a string.
Examples
1. Change background color using configure method
In this example, we will change the Tkinter window’s background color to blue.
Python Program
from tkinter import * gui = Tk(className='Python Examples - Window Color') # set window size gui.geometry("400x200") #set window color gui.configure(bg='blue') gui.mainloop()
2. Change background color using bg property
In this example, we will change the Tkinter window’s background color to blue.
Python Program
from tkinter import * gui = Tk(className='Python Examples - Window Color') # set window size gui.geometry("400x200") #set window color gui['bg']='green' gui.mainloop()
3. Using background for bg
You can use the property name bg as in the above two examples, or also use the full form background. In this example, we will use the full form background to set the background color of Tkinter window.
Python Program
from tkinter import * gui = Tk(className='Python Examples - Window Color') # set window size gui.geometry("400x200") #set window color gui['background']='yellow' #gui.configure(background='yellow') gui.mainloop()
4. Using HEX code for background color
As already mentioned during the start, you can provide a HEX equivalent value for a color. In this example, we provide a HEX value to color and check the output.
Python Program
from tkinter import * gui = Tk(className='Python Examples - Window Color') # set window size gui.geometry("400x200") #set window color gui['background']='#856ff8' gui.mainloop()
Summary
In this tutorial of Python Examples, we learned how to change the background color of Tkinter GUI window, with the help of well detailed Python example programs.
Set Tkinter Background Color
- Method configure(background= )
- Attribute bg or background
- Tkinter Color Code
Tkinter widget class method configure and attribute bg or background could be used to set the Tkinter widget/window background color.
Method configure(background= )
try: import Tkinter as tk except: import tkinter as tk app = tk.Tk() app.title("configure method") app.geometry('300x200') app.configure(bg='red') app.mainloop()
app.configure(bg=’red’) configures the background color of app to be red . You could also use background instead of bg .
Attribute bg or background
background or bg is one attribute in most of Tkinter widgets, and could be used to set the background color directly.
try: import Tkinter as tk except: import tkinter as tk app = tk.Tk() app.title("bg attribute") app.geometry('300x200') app['bg'] = 'blue' app.mainloop()
Tkinter Color Code
You could specify the color with the known names like red , blue or green as shown above, and you could also specify it via RGB like hex color codes in HTML, e.g, #49A or #0059b3 .
try: import Tkinter as tk except: import tkinter as tk app = tk.Tk() app.title("bg attribute") app.geometry('300x200') app['bg'] = '#0059b3' app.mainloop()
try: import Tkinter as tk except: import tkinter as tk app = tk.Tk() app.title("bg attribute") app.geometry('300x200') app['bg'] = '#49A' app.mainloop()
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
How to change Tkinter Button Background Color?
You can change the background color of a Tkinter Button by setting the bg property of Tkinter Button with a color string or HEX value.
Assign any standard color or rgb hex value to the bg property as shown below.
button = Button(tkWindow, bg='blue') button = Button(tkWindow, bg='black') button = Button(tkWindow, bg='white') button = Button(tkWindow, bg='red') #hex values button = Button(tkWindow, bg='#54FA9B') button = Button(tkWindow, bg='#A877BA')
The default color of Tkinter Button is grey. In this tutorial, we will learn how to change the color of Tkinter Button.
Examples
1. Set Tkinter Button color to blue
In this example, we have set the Button’s background color to blue by assigning named property bg in Button() class to blue.
Python Program
from tkinter import * tkWindow = Tk() tkWindow.geometry('400x150') tkWindow.title('Button Background Example') button = Button(tkWindow, text = 'Submit', bg='blue', fg='white') button.pack() tkWindow.mainloop()
2. Set Tkinter Button background color to black
In this example, we have set the background color of the button to black with bg=’black’.
Python Program
from tkinter import * tkWindow = Tk() tkWindow.geometry('400x150') tkWindow.title('Button Background Example') button = Button(tkWindow, text = 'Submit', bg='black', fg='white') button.pack() tkWindow.mainloop()
3. Set Tkinter Button background color to Hex color
You can also assign any RGB equivalent hex color to the bg property.
In this example, we assigned bg=’#ffb3fe’.
Python Program
from tkinter import * tkWindow = Tk() tkWindow.geometry('400x150') tkWindow.title('Button Background Example') button = Button(tkWindow, text = 'Submit', bg='#ffb3fe') button.pack() tkWindow.mainloop()
Summary
In this tutorial of Python Examples, we learned to set the background color of Tkinter button.