How to use rgb color codes in tkinter?

Tkinter provides flexible color customization through both named colors and RGB hex codes. RGB (Red, Green, Blue) color codes use hexadecimal values to define precise colors for widgets like backgrounds, text, and borders.

To use RGB color codes in Tkinter, define them using the format #RRGGBB where each pair represents red, green, and blue values from 00 to FF (0-255 in decimal).

Basic RGB Color Syntax

RGB colors in Tkinter follow this format ?

import tkinter as tk

root = tk.Tk()
root.geometry("400x200")

# RGB color format: #RRGGBB
root.configure(bg='#FF5733')  # Orange-red background

label = tk.Label(root, text="RGB Color Example", 
                bg='#3498DB',    # Blue background
                fg='#FFFFFF',    # White text
                font=('Arial', 16))
label.pack(pady=50)

root.mainloop()

Common RGB Color Examples

Here are some commonly used RGB color codes ?

import tkinter as tk

root = tk.Tk()
root.geometry("500x300")
root.configure(bg='#F0F0F0')  # Light gray background

# Create labels with different RGB colors
colors = [
    ('#FF0000', 'Red'),
    ('#00FF00', 'Green'), 
    ('#0000FF', 'Blue'),
    ('#FFD700', 'Gold'),
    ('#800080', 'Purple'),
    ('#FFA500', 'Orange')
]

for i, (color, name) in enumerate(colors):
    label = tk.Label(root, text=f"{name}: {color}", 
                    bg=color, fg='white' if color != '#FFD700' else 'black',
                    font=('Arial', 12), width=20)
    label.pack(pady=5)

root.mainloop()

Using RGB with Different Widgets

RGB colors work with all Tkinter widgets ?

import tkinter as tk

root = tk.Tk()
root.geometry("600x400")
root.configure(bg='#2C3E50')  # Dark blue-gray

# Button with RGB colors
button = tk.Button(root, text="Click Me!", 
                  bg='#E74C3C',     # Red background
                  fg='#FFFFFF',     # White text
                  activebackground='#C0392B',  # Darker red when pressed
                  font=('Arial', 14, 'bold'),
                  relief='flat',
                  padx=20, pady=10)
button.pack(pady=20)

# Entry widget
entry = tk.Entry(root, bg='#ECF0F1', fg='#2C3E50', 
                font=('Arial', 12), width=30)
entry.pack(pady=10)
entry.insert(0, "RGB colored entry field")

# Frame with RGB background
frame = tk.Frame(root, bg='#9B59B6', width=300, height=100)
frame.pack(pady=20)
frame.pack_propagate(False)

frame_label = tk.Label(frame, text="Purple Frame", 
                      bg='#9B59B6', fg='white', 
                      font=('Arial', 14))
frame_label.pack(expand=True)

root.mainloop()

RGB Color Picker Function

You can create a function to convert RGB values to hex format ?

import tkinter as tk

def rgb_to_hex(r, g, b):
    """Convert RGB values (0-255) to hex color code"""
    return f"#{r:02x}{g:02x}{b:02x}"

root = tk.Tk()
root.geometry("400x300")

# Using RGB values directly
red_color = rgb_to_hex(231, 76, 60)    # RGB(231, 76, 60) to #E74C3C
blue_color = rgb_to_hex(52, 152, 219)  # RGB(52, 152, 219) to #3498DB

print(f"Red color: {red_color}")
print(f"Blue color: {blue_color}")

# Apply colors to widgets
header = tk.Label(root, text="RGB to Hex Conversion", 
                 bg=red_color, fg='white', 
                 font=('Arial', 16, 'bold'), pady=20)
header.pack(fill='x')

content = tk.Label(root, text="This uses converted RGB values", 
                  bg=blue_color, fg='white', 
                  font=('Arial', 12), pady=30)
content.pack(fill='x', padx=20, pady=20)

root.mainloop()

Common RGB Color Codes

Color Name RGB Code RGB Values
Red #FF0000 255, 0, 0
Green #00FF00 0, 255, 0
Blue #0000FF 0, 0, 255
White #FFFFFF 255, 255, 255
Black #000000 0, 0, 0

Conclusion

RGB color codes in Tkinter use the #RRGGBB format where each pair represents red, green, and blue values in hexadecimal. This provides precise color control for all widget properties like backgrounds, text colors, and borders.

Updated on: 2026-03-25T22:29:05+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements