Python 3 – Tkinter Colors
When it comes to creating graphical user interfaces (GUI), the library that comes to mind for Python developers is often Tkinter
. Among its many utilities, Tkinter
also includes a rich color palette that can be used to enhance the aesthetics and usability of the graphical interfaces we create. In this article, we’ll explore the different options for specifying and using color in Tkinter
.
Specifying color in Tkinter
Before we dive deeper into the different ways we can use color in Tkinter
, let’s first understand the different ways we can specify color values in Python and Tkinter
.
There are a few standard color notation conventions that can be used in Tkinter
, including:
- Hex: the six digit hexadecimal notation (#RRGGBB), where each of the colors is represented by a two-digit hex value (00-FF).
- RGB: the RGB color model which consists of 3 values (red, green and blue) ranging from 0-255.
- Name: a named color value, such as “red”, “green” etc.
Now let’s look at how we can use these notations to specify colors in Tkinter.
To specify a color, we simply pass the color value as a string to the widget’s bg
, foreground
or background
attributes.
import tkinter as tk
root = tk.Tk()
# Using hex notation
button1 = tk.Button(root, text="Hex", bg="#FF0000") # Red button background
button2 = tk.Button(root, text="Hex", bg="#00FF00") # Green button background
button3 = tk.Button(root, text="Hex", bg="#0000FF") # Blue button background
# Using named color notation
button4 = tk.Button(root, text="Named Color", bg="orange")
button5 = tk.Button(root, text="Named Color", bg="gray")
# Using RGB notation
button6 = tk.Button(root, text="RGB", bg="rgb(255,255,51)")
button7 = tk.Button(root, text="RGB", bg="rgb(0,128,0)")
button1.pack(pady=5)
button2.pack(pady=5)
button3.pack(pady=5)
button4.pack(pady=5)
button5.pack(pady=5)
button6.pack(pady=5)
button7.pack(pady=5)
root.mainloop()
In the code above, we create seven buttons using different color notations. You can easily identify which notation has been used by the color value we pass to the bg
attribute.
Tkinter Colors
As we mentioned before, Tkinter
has a significant color palette that includes many standard named colors. These colors can be directly specified using the color’s name as a string value. Here is a list of the standard Tkinter colors with sample code:
import tkinter as tk
root = tk.Tk()
colors = [["alice blue", "#f0f8ff"], ["antique white", "#faebd7"],
["aquamarine", "#7fffd4"], ["azure", "#f0ffff"],
["beige", "#f5f5dc"], ["bisque", "#ffe4c4"],
["black", "#000000"], ["blue", "#0000ff"],
["blue violet", "#8a2be2"], ["brown", "#a52a2a"],
["burlywood", "#deb887"], ["cadet blue", "#5f9ea0"],
["chartreuse", "#7fff00"], ["chocolate", "#d2691e"],
["coral", "#ff7f50"], ["cornflower blue", "#6495ed"],
["cornsilk", "#fff8dc"], ["crimson", "#dc143c"],
["cyan", "#00ffff"], ["dark blue", "#00008b"],
["dark cyan", "#008b8b"], ["dark goldenrod", "#b8860b"],
["dark gray", "#a9a9a9"], ["dark green", "#006400"],
["dark grey", "#a9a9a9"], ["dark khaki", "#bdb76b"],
["dark magenta", "#8b008b"], ["dark olive green", "#556b2f"],
["dark orange", "#ff8c00"], ["dark orchid", "#9932cc"],
["dark red", "#8b0000"], ["dark salmon", "#e9967a"],
["dark sea green", "#8fbc8f"], ["dark slate blue", "#483d8b"],
["dark slate gray", "#2f4f4f"], ["dark slate grey", "#2f4f4f"],
["dark turquoise", "#00ced1"], ["dark violet", "#9400d3"],
["deep pink", "#ff1493"], ["deep sky blue", "#00bfff"],
["dim gray", "#696969"], ["dim grey", "#696969"],
["dodger blue", "#1e90ff"], ["firebrick", "#b22222"],
["floral white", "#fffaf0"], ["forest green", "#228b22"],
["fuchsia", "#ff00ff"], ["gainsboro", "#dcdcdc"],
["ghost white", "#f8f8ff"], ["gold", "#ffd700"],
["goldenrod", "#daa520"], ["gray", "#808080"],
["grey", "#808080"], ["green", "#008000"],
["green yellow", "#adff2f"], ["honeydew", "#f0fff0"],
["hot pink", "#ff69b4"], ["indian red", "#cd5c5c"],
["ivory", "#fffff0"], ["khaki", "#f0e68c"],
["lavender", "#e6e6fa"], ["lavender blush", "#fff0f5"],
["lawn green", "#7cfc00"], ["lemon chiffon", "#fffacd"],
["light blue", "#add8e6"], ["light coral", "#f08080"],
["light cyan", "#e0ffff"], ["light goldenrod", "#fafad2"],
["light goldenrod yellow", "#fafad2"], ["light gray", "#d3d3d3"],
["light green", "#90ee90"], ["light grey", "#d3d3d3"],
["light pink", "#ffb6c1"], ["light salmon", "#ffa07a"],
["light sea green", "#20b2aa"], ["light sky blue", "#87cefa"],
["light slate blue", "#8470ff"], ["light slate gray", "#778899"],
["light slate grey", "#778899"], ["light steel blue", "#b0c4de"],
["light yellow", "#ffffe0"], ["lime", "#00ff00"],
["lime green", "#32cd32"], ["linen", "#faf0e6"],
["magenta", "#ff00ff"], ["maroon", "#800000"],
["medium aquamarine", "#66cdaa"], ["medium blue", "#0000cd"],
["medium orchid", "#ba55d3"], ["medium purple", "#9370db"],
["medium sea green", "#3cb371"], ["medium slate blue", "#7b68ee"],
["medium spring green", "#00fa9a"], ["medium turquoise", "#48d1cc"],
["medium violet red", "#c71585"], ["midnight blue", "#191970"],
["mint cream", "#f5fffa"], ["misty rose", "#ffe4e1"],
["moccasin", "#ffe4b5"], ["navajo white", "#ffdead"],
["navy", "#000080"], ["old lace", "#fdf5e6"],
["olive", "#808000"], ["olive drab", "#6b8e23"], ["orange", "#ffa500"],
["orange red", "#ff4500"], ["orchid", "#da70d6"],
["pale goldenrod", "#eee8aa"], ["pale green", "#98fb98"],
["pale turquoise", "#afeeee"], ["pale violet red", "#d87093"],
["papaya whip", "#ffefd5"], ["peach puff", "#ffdab9"],
["peru", "#cd853f"], ["pink", "#ffc0cb"], ["plum", "#dda0dd"],
["powder blue", "#b0e0e6"], ["purple", "#800080"],
["red", "#ff0000"], ["rosy brown", "#bc8f8f"],
["royal blue", "#4169e1"], ["saddle brown", "#8b4513"],
["salmon", "#fa8072"], ["sandy brown", "#f4a460"],
["sea green", "#2e8b57"], ["seashell", "#fff5ee"],
["sienna", "#a0522d"], ["silver", "#c0c0c0"],
["sky blue", "#87ceeb"], ["slate blue", "#6a5acd"],
["slate gray", "#708090"], ["slate grey", "#708090"],
["snow", "#fffafa"], ["spring green", "#00ff7f"],
["steel blue", "#4682b4"], ["tan", "#d2b48c"], ["teal", "#008080"],
["thistle", "#d8bfd8"], ["tomato", "#ff6347"], ["turquoise", "#40e0d0"],
["violet", "#ee82ee"], ["wheat", "#f5deb3"], ["white", "#ffffff"],
["white smoke", "#f5f5f5"], ["yellow", "#ffff00"],
["yellow green", "#9acd32"]]
for name, value in colors:
tk.Label(root, text=name, bg=value).pack()
root.mainloop()
In the sample code above, we use a loop to create a label for each standard color and set its background color to the corresponding value.
Tkinter Color Chooser
Sometimes, we might want to allow users to select their desired color for a specific purpose such as customizing themes or changing the appearance of the interface. In such a scenario, we can use the Color Chooser
dialog box provided by Tkinter
. The Code fragment below illustrates how one can choose a color using the ColorChooser
dialog:
import tkinter as tk
from tkinter import colorchooser
root = tk.Tk()
def choose_color():
color = colorchooser.askcolor(title="Choose a color")
print(color)
button = tk.Button(root, text="Select Color", command=choose_color)
button.pack()
root.mainloop()
In the code above, we create a button with a choose_color
function as its command. In the choose_color
function, we simply call the colorchooser.askcolor
function to get a tuple containing the selected color value and the color name.
Conclusion
In conclusion, we’ve seen the different ways we can specify color values in Tkinter
, including using hex, RGB and named notations. We’ve also seen the standard Tkinter colors palette and how they can be used directly in widgets. Finally, we’ve discovered how to use the Color Chooser
dialog box to allow users to select their desired color. In summary, adding color to the interfaces we create using Tkinter
can greatly enhance their appearance and usability, and with the tools and options that Tkinter
provides, we have everything we need to do so.