Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Default window color Tkinter and hex color codes in Tkinter
A Tkinter window can be customized by adding properties and attributes such as background color, foreground color, width, height, etc.
The color attribute in config() defines the default color of the main window. We can set the color of the window by defining either Hex Color (e.g., #000 for Black) or the Name of the color.
Setting Background Color with Hex Codes
Hex color codes provide precise color control using hexadecimal values. The format is #RRGGBB where RR, GG, and BB represent red, green, and blue components ?
# Import the required libraries
from tkinter import *
# Create an instance of Tkinter Frame
win = Tk()
# Set the geometry
win.geometry("700x350")
# Set the default color of the window using hex code
win.config(bg='#24f3f0') # Cyan-like color
Label(win, text="Hey There! Welcome to TutorialsPoint",
font=('Helvetica', 22, 'bold'),
foreground="navy").pack(pady=50)
win.mainloop()
Setting Background Color with Color Names
Tkinter also supports standard color names for easier readability ?
from tkinter import *
win = Tk()
win.geometry("700x350")
# Set background using color name
win.config(bg='SkyBlue1')
Label(win, text="Hey There! Welcome to TutorialsPoint",
font=('Helvetica', 22, 'bold'),
foreground="navy").pack(pady=50)
win.mainloop()
Common Hex Color Codes
Here are some commonly used hex color codes for Tkinter applications:
| Color | Hex Code | Color Name Alternative |
|---|---|---|
| White | #FFFFFF | white |
| Black | #000000 | black |
| Light Blue | #87CEEB | SkyBlue |
| Light Gray | #D3D3D3 | LightGray |
| Navy Blue | #000080 | navy |
Example with Multiple Color Options
This example demonstrates switching between different background colors ?
from tkinter import *
win = Tk()
win.geometry("600x400")
win.title("Color Demo")
# Try different background colors
colors = ['#FFB6C1', '#98FB98', '#F0E68C', '#DDA0DD']
current_color = 0
def change_color():
global current_color
win.config(bg=colors[current_color])
current_color = (current_color + 1) % len(colors)
win.config(bg=colors[0]) # Start with first color
Label(win, text="Click to Change Background Color",
font=('Arial', 16, 'bold')).pack(pady=20)
Button(win, text="Change Color", command=change_color,
font=('Arial', 12), bg='white').pack(pady=10)
win.mainloop()
Conclusion
Tkinter windows can be customized using either hex color codes (#RRGGBB) for precise control or standard color names for simplicity. Use win.config(bg='color') to set the background color of your application window.
