Updated June 28, 2023
Definition of Tkinter Color Chart
Tkinter color chart is one of the modules of Tkinter library. It allows programmers to choose a color of their choice easily from the already available set of bulk colors for developing an enhanced GUI. Tkinter color chart with a range of all the varieties of color is being devised by Tkinter and Hortsmann graphics library available in Python, which is extensively used for identifying different colors. A semantic program can be used to generate and create the Tkinter color chart, which is quite advantageous compared to the direct usage of Tkinter color for creating an enhanced GUI.
Syntax:
Since Tkinter color chart is an extension of the Tkinter colors, it is represented as follows :
Import tkinter as tk
Colors = [
list of colors as per color chart
]
Call for the necessary methods;
mainloop();
The syntax flow has significance in the way that the Tkinter module represented as tk is used for importing the necessary packages. Then the variable colors are declared, which contain all the necessary colors as per the chart, which is then called by any of the methods as per requirement. Finally, the mainloop method is called to put all the overlapping changes.
How to Create Color Chart in Tkinter?
There is not much of a difference in the Tkinter module when incorporated with colors. However, still, very little difference exists with the fact that it can be called an extension to the colors attribute means that the Tkinter color chart is an extension to the color attribute of the Tkinter standard library, providing an ability to the programmers or developers to choose any random color of choice at one go. Let’s check the creation of a color chart using Tkinter in a bit more detail.
- Import the Tkinter module or the standard library tk to support the functionality.
- Declare and define the variable representing colors in the chart format I.e. declare the Tkinter color chart containing colors like ‘gray,’ ‘green,’ ‘cyan,’ ‘violet,’ etc. It will contain colors including colors in RGB[Red, Green, Blue] color with a normal format.
- Call for any of the methods like sorting the colors or arranging the color in a format like grid method, pack method, etc.
- And End by calling the mainloop, which will apply the necessary changes on top of it for the changes and stuff to be continually working.
Constructor
There is no specific constructor to be passed for any specific method, only for the Tkinter color chart. It all depends on the method that needs to be used for a specific reason, which signifies when values are passed on to the variable to be used for the application creation. That application completely depends on the random selection of colors present on the color chart. So, a list of mandatory colors as part of the Tkinter tk module is mandatory and is mentioned in many of the Tkinter-based color charts. Taking examples and elicitation might simplify the use of constructors.
- bg: to set any background color normally in the entire canvas.
- activebackground: to set the background color once the widget is decided to be placed under the cursor.
- activeforeground: to set the foreground color once the widget is decided to be placed under the cursor.
- Highlightcolor: This is the parameter related to color, which is often used with texts to highlight any specific text with color.
Methods
Certain methods need to be used frequently when color charts are used for implementation. These color charts are mostly used and emphasized for creating gaming applications.
The following methods are used for the creation of Tkinter color chart-based applications:
- Pack() method
- Grid() method
- Place() method
# Pack() Method: The pack() method is used to organize widgets in a block format before placing them in the parent or main widget.
# Grid() Method: The grid() method, part of the standard Tkinter library, is used to organize widgets in a grid-like structure, similar to a table, before placing them in the parent or main window.
# Place() Method: The place() method, another module of the Tkinter library, is used to organize widgets by placing them in specific positions and paths as instructed by the programmer, based on their requirements.
Example of Tkinter Color Chart
This program demonstrates the clear usage of the Tkinter color chart, which can be used appropriately as per program requirements, as shown in the output.
Code:
import tkinter as tk
COL_ORS = [
'IndianRed1','firebrick1', 'coral1', 'HotPink2', 'brown1',
'red2', 'gray1', 'VioletRed1', 'purple2', 'goldenrod1', 'SeaGreen1' , 'OliveDrab2', 'DarkGoldenrod1', 'DarkOrchid3',
'floral white', 'magenta4', 'pink4', 'LightYellow3', 'LightCyan3', 'turquoise2', 'OliveDrab4', 'thistle4'
]
class Color_Chart(tk.Frame):
Mx_Rw = 32
Fnt_Sz = 12
def __init__(slf, r_t):
tk.Frame.__init__(slf, r_t)
rw = 0
cl = 0
for color in COL_ORS:
label = tk.Label(slf, text=color, bg=color,
font=("Times", slf.Fnt_Sz, "bold"))
label.grid(row=rw, column=cl, sticky="ew")
rw += 1
if rw > slf.Mx_Rw:
rw = 0
cl += 1
slf.pack(expand=1, fill="both")
if __name__ == '__main__':
r_t = tk.Tk()
r_t.title("Tkinter_Color_Chart")
app = Color_Chart(r_t)
r_t.mainloop()
Output:
Recommended Articles
This is a guide to Tkinter Color Chart. Here we discuss the Definition, How to Create Color Chart in Tkinter with sample code for better understanding. You may also have a look at the following articles to learn more –