- Python Tkinter Colors + Example
- Python Tkinter Colors
- Python Tkinter Colors RGB
- Python Tkinter Color Text
- Python Tkinter Color Button
- Python Tkinter Color Chooser
- Python Tkinter Color Label
- Python Tkinter Color Transparent
- Python Tkinter Color Window
- List of named colors#
- Base colors#
- Tableau Palette#
- rene-d / colors.py
Python Tkinter Colors + Example
In this Python tutorial, we will learn how to implement colors using Python Tkinter. Let us understand in detail Python Tkinter colors with the points:
- Python Tkinter Colors
- Python Tkinter Colors RGB
- Python Tkinter Color Text
- Python Tkinter Color Button
- Python Tkinter Color Chooser
- Python Tkinter Color Label
- Python Tkinter Color Transparent
- Python Tkinter Color Window
Python Tkinter Colors
Colors play an important role in the UI/UX of the application. It adds a professional touch to the application and helps in engaging the user for a longer time.
- Every color has some thought behind it, selecting the right color for an application is an art & it takes time to master it.
- Red symbolizes an error, green means okay or done, yellow means something is incomplete, etc.
- Every color has two things and the developer/user can provide either of them:
- color name: ‘white smoke’, ‘red’, ‘floral white’, ‘blue’, etc
- color code: #F5F5F5, #FF0000, #FFFAF0, 0000FF, etc
Python Tkinter Colors RGB
Python Tkinter does not support input of colors in RGB format but we can create a workaround that accepts inputs in a RGB format.
- In the below program, you can notice that we have created a function that accepts user input and it adds pound sign (#) as prefix and byte code as a suffix.
- Now while providing the background color we have used this function and provided the RGB values.
from tkinter import * def rgb_hack(rgb): return "#%02x%02x%02x" % rgb ws = Tk() ws.title('PythonGuides') ws.geometry('400x300') ws.config(bg=rgb_hack((255, 0, 122))) ws.mainloop()
In this output, you can see that window has a color passed in the code in a RGB format.
Python Tkinter Color Text
In this section, we will learn how to set the color of the Text in Python Tkinter.
- foreground or fg is the option that accepts the color input from the user and sets the font or text color.
- This option is common in almost all the widgets and can be applied on the text of any widget.
- In our example, we will be implementing color on the Text box widget. By default the Text color is blue but we will change it to Blue.
from tkinter import * ws = Tk() ws.title('PythonGuides') ws.config(bg='#D9D8D7') ws.geometry('400x300') tb = Text( ws, width=25, height=8, font=('Times', 20), wrap='word', fg='#4A7A8C' ) tb.pack(expand=True) ws.mainloop()
Output of Python Tkinter Color Text
In this output, we have changed the default color of the text from black to light blue.
Python Tkinter Color Button
In this section, we will learn how to change the color of the Button and button text in Python Tkinter.
- Button widget in Python Tkinter has mainly three colors applied on it.
- Button Text Color
- Button background Color
- Button color when clicked
from tkinter import * ws = Tk() ws.titlSelect colorSelect colore('PythonGuides') ws.config(bg='#D9D8D7') ws.geometry('400x300') Button( ws, text='Download', font=('Times', 20), padx=10, pady=10, bg='#4a7abc', fg='yellow', activebackground='green', activeforeground='white' ).pack(expand=True) ws.mainloop()
In this output, a button has a background color as light-blue and yellow text. When the mouse is hovered-over it, the button changes to a green background and white foreground or text color.
Python Tkinter Color Chooser
In this section, we will learn to create a color chooser using python tkinter.
- Python Tkinter provides colorchooser module using which color toolbox can be displayed.
- Color chooser allows users to choose colors from the color tray.
- colorchooser returns the RGB code with hexadecimal color code in the tuple format.
- In our example, we will create an application that allows users to choose any color from the color tray, and then that color will be displayed on the label.
from tkinter import * from tkinter import colorchooser def pick_color(): color = colorchooser.askcolor(title ="Choose color") color_me.config(bg=color[1]) color_me.config(text=color) ws = Tk() ws.title('PythonGuides') ws.geometry('400x300') color_me = Label( ws, text='(217, 217, 217) #d9d9d9', font = ('Times', 20), relief = SOLID, padx=20, pady=20 ) color_me.pack(expand=True) button = Button( ws, text = "Choose Color", command = pick_color, padx=10, pady=10, font=('Times', 18), bg='#4a7a8c' ) button.pack() ws.mainloop()
In this output, you can see that when the user clicks on the choose color button then a color tray is pop-up. and the label changes color to the selected color.
This is how to use a color chooser in Python Tkinter.
Python Tkinter Color Label
In this section, we will learn how to change color of a Label widget in Python Tkinter.
- Label in Python Tkinter is a widget that is used to display text and images on the application.
- We can apply color on the Label widget and Label Text.
- To color the widget label, the background or bg keyword is used, and to change the text color of the label widget, the foreground or fg keyword is used.
- In this example, we have a colored label widget and label text.
from tkinter import * ws = Tk() ws.title('PythonGuides') ws.config(bg='#D9D8D7') ws.geometry('400x300') Label( ws, text='This text is displayed \n using Label Widget', font=('Times', 20), padx=10, pady=10, bg='#4a7abc', fg='yellow' ).pack(expand=True) ws.mainloop()
In this output, Label widget is painted with light blue and Label text with yellow
Python Tkinter Color Transparent
In this section, we will learn how to set the color to transparent in Python Tkinter. In other words, removing all the colors from the background so that background things start appearing through the window.
- The first step in the process is to set the background color to the window using the config keyword.
- now provide the same background color in the wm_attributes()
- Here is the implementation of Python Tkinter Color Transparent.
from tkinter import * ws = Tk() ws.title('PythonGuides') ws.geometry('400x300') ws.config(bg='#4a7a8c') ws.wm_attributes('-transparentcolor','#4a7a8c') ws.mainloop()
Here is the output of Python Tkinter Color Transparent
In this output, the application is transparent and the color you are seeing is the background color of the desktop.
Python Tkinter Color Window
In this section, we will learn how to color the main window of Python Tkinter.
- In Python Tkinter, we can change the default settings of the widget by changing the configuration.
- To change the configuration we can use the keyword config or configure.
from tkinter import * ws = Tk() ws.title('PythonGuides') ws.geometry('400x300') ws.config(bg='#116562') ws.mainloop()
In this output, window color is changed displayed. Default color of Python Tkinter window is Light Gray, but we have changed it to see green.
You may like the following Python Tkinter tutorials:
In this tutorial, we have learned how to use color in Python Tkinter. Also, we have covered these topics.
- Python Tkinter Colors
- Python Tkinter Colors Example
- Python Tkinter Colors RGB
- Python Tkinter Color Text
- Python Tkinter Color Button
- Python Tkinter Color Chooser
- Python Tkinter Color Label
- Python Tkinter Color Transparent
- Python Tkinter Color Window
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.
List of named colors#
First we define a helper function for making a table of colors, then we use it on some common color categories.
import math from matplotlib.patches import Rectangle import matplotlib.pyplot as plt import matplotlib.colors as mcolors def plot_colortable(colors, *, ncols=4, sort_colors=True): cell_width = 212 cell_height = 22 swatch_width = 48 margin = 12 # Sort colors by hue, saturation, value and name. if sort_colors is True: names = sorted( colors, key=lambda c: tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(c)))) else: names = list(colors) n = len(names) nrows = math.ceil(n / ncols) width = cell_width * 4 + 2 * margin height = cell_height * nrows + 2 * margin dpi = 72 fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi) fig.subplots_adjust(margin/width, margin/height, (width-margin)/width, (height-margin)/height) ax.set_xlim(0, cell_width * 4) ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.) ax.yaxis.set_visible(False) ax.xaxis.set_visible(False) ax.set_axis_off() for i, name in enumerate(names): row = i % nrows col = i // nrows y = row * cell_height swatch_start_x = cell_width * col text_pos_x = cell_width * col + swatch_width + 7 ax.text(text_pos_x, y, name, fontsize=14, horizontalalignment='left', verticalalignment='center') ax.add_patch( Rectangle(xy=(swatch_start_x, y-9), width=swatch_width, height=18, facecolor=colors[name], edgecolor='0.7') ) return fig
Base colors#
Tableau Palette#
rene-d / colors.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# SGR color constants # rene-d 2018 class Colors : «»» ANSI color codes «»» BLACK = » \033 [0;30m» RED = » \033 [0;31m» GREEN = » \033 [0;32m» BROWN = » \033 [0;33m» BLUE = » \033 [0;34m» PURPLE = » \033 [0;35m» CYAN = » \033 [0;36m» LIGHT_GRAY = » \033 [0;37m» DARK_GRAY = » \033 [1;30m» LIGHT_RED = » \033 [1;31m» LIGHT_GREEN = » \033 [1;32m» YELLOW = » \033 [1;33m» LIGHT_BLUE = » \033 [1;34m» LIGHT_PURPLE = » \033 [1;35m» LIGHT_CYAN = » \033 [1;36m» LIGHT_WHITE = » \033 [1;37m» BOLD = » \033 [1m» FAINT = » \033 [2m» ITALIC = » \033 [3m» UNDERLINE = » \033 [4m» BLINK = » \033 [5m» NEGATIVE = » \033 [7m» CROSSED = » \033 [9m» END = » \033 [0m» # cancel SGR codes if we don’t write to a terminal if not __import__ ( «sys» ). stdout . isatty (): for _ in dir (): if isinstance ( _ , str ) and _ [ 0 ] != «_» : locals ()[ _ ] = «» else : # set Windows console in VT mode if __import__ ( «platform» ). system () == «Windows» : kernel32 = __import__ ( «ctypes» ). windll . kernel32 kernel32 . SetConsoleMode ( kernel32 . GetStdHandle ( — 11 ), 7 ) del kernel32 if __name__ == ‘__main__’ : for i in dir ( Colors ): if i [ 0 : 1 ] != «_» and i != «END» : print ( «16> <>» . format ( i , getattr ( Colors , i ) + i + Colors . END )) This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#! /bin/bash # rene-d 2015 # définition de constantes SGR pour affichage de couleur # exemple: echo -e $rouge$ if ! tty -s ; then # ne pas changer l’indentation des lignes suivantes COLOR_BLACK= » « COLOR_RED= » « COLOR_GREEN= » « COLOR_BROWN= » « COLOR_BLUE= » « COLOR_PURPLE= » « COLOR_CYAN= » « COLOR_LIGHT_GRAY= » « COLOR_DARK_GRAY= » « COLOR_LIGHT_RED= » « COLOR_LIGHT_GREEN= » « COLOR_YELLOW= » « COLOR_LIGHT_BLUE= » « COLOR_LIGHT_PURPLE= » « COLOR_LIGHT_CYAN= » « COLOR_LIGHT_WHITE= » « COLOR_BOLD= » « COLOR_FAINT= » « COLOR_ITALIC= » « COLOR_UNDERLINE= » « COLOR_BLINK= » « COLOR_NEGATIVE= » « COLOR_CROSSED= » « COLOR_END= » « else # ne pas indenter les lignes suivantes (pour le sed suivant) COLOR_BLACK= » \033[0;30m « COLOR_RED= » \033[0;31m « COLOR_GREEN= » \033[0;32m « COLOR_BROWN= » \033[0;33m « COLOR_BLUE= » \033[0;34m « COLOR_PURPLE= » \033[0;35m « COLOR_CYAN= » \033[0;36m « COLOR_LIGHT_GRAY= » \033[0;37m « COLOR_DARK_GRAY= » \033[1;30m « COLOR_LIGHT_RED= » \033[1;31m « COLOR_LIGHT_GREEN= » \033[1;32m « COLOR_YELLOW= » \033[1;33m « COLOR_LIGHT_BLUE= » \033[1;34m « COLOR_LIGHT_PURPLE= » \033[1;35m « COLOR_LIGHT_CYAN= » \033[1;36m « COLOR_LIGHT_WHITE= » \033[1;37m « COLOR_BOLD= » \033[1m « COLOR_FAINT= » \033[2m « COLOR_ITALIC= » \033[3m « COLOR_UNDERLINE= » \033[4m « COLOR_BLINK= » \033[5m « COLOR_NEGATIVE= » \033[7m « COLOR_CROSSED= » \033[9m « # ne pas changer l’indentation des lignes suivantes COLOR_END= » \033[0m « fi # affiche les couleurs if [ » $1 » = » -h » ] ; then for i in ` cat $0 | sed -e ‘ s/^\(COLOR.*\)=\(.*\)/\1/p;d ‘ ` ; do [ » $i » != » COLOR_END » ] && echo -e $ < ! i>$i $ done fi