Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
The tkinter.colorchooser
module provides a dialog window to let the user choose a color. In this tutorial, we'll see how to use this dialog to choose a color and then apply that color to a tkinter widget.
tkinter
Tutorial:1. Import Required Libraries:
import tkinter as tk from tkinter import colorchooser
2. Function to Trigger the Color Dialog:
To open the color dialog and get the color, you can use the askcolor()
function provided by colorchooser
. This function will return a tuple where the first item is a tuple representing the RGB values of the selected color and the second item is the color in the string format.
Here's how you can open the color chooser and get the selected color:
def choose_color(): color = colorchooser.askcolor(title ="Choose a color") if color[1]: label.config(bg=color[1])
3. Create the Main Application Window and Widgets:
root = tk.Tk() root.title("Color Chooser Tutorial") label = tk.Label(root, text="Color me!", width=30, height=10) label.pack(pady=20) btn = tk.Button(root, text="Choose Color", command=choose_color) btn.pack(pady=20) root.mainloop()
Complete Code:
import tkinter as tk from tkinter import colorchooser def choose_color(): color = colorchooser.askcolor(title ="Choose a color") if color[1]: label.config(bg=color[1]) root = tk.Tk() root.title("Color Chooser Tutorial") label = tk.Label(root, text="Color me!", width=30, height=10) label.pack(pady=20) btn = tk.Button(root, text="Choose Color", command=choose_color) btn.pack(pady=20) root.mainloop()
In this code, when you run the program, you'll see a label with a "Choose Color" button below it. When you click the button, the color chooser dialog will appear. Once you select a color and click OK, the background of the label will change to the chosen color. If you cancel the dialog, no changes will be made.
Python Tkinter color picker example:
import tkinter as tk from tkinter import colorchooser def pick_color(): color = colorchooser.askcolor(title="Choose a color")[1] print("Selected color:", color) root = tk.Tk() color_button = tk.Button(root, text="Pick a Color", command=pick_color) color_button.pack() root.mainloop()
How to use the color chooser in Tkinter:
colorchooser
module provides a convenient way to integrate a color chooser dialog in your application.import tkinter as tk from tkinter import colorchooser def pick_color(): color = colorchooser.askcolor(title="Choose a color")[1] print("Selected color:", color) root = tk.Tk() color_button = tk.Button(root, text="Pick a Color", command=pick_color) color_button.pack() root.mainloop()
Choosing colors for widgets in Tkinter:
import tkinter as tk from tkinter import colorchooser def apply_color(): color = colorchooser.askcolor(title="Choose a color")[1] label.config(text="Selected color: " + color) button.config(bg=color) root = tk.Tk() label = tk.Label(root, text="Selected color: ") label.pack() button = tk.Button(root, text="Apply Color", command=apply_color) button.pack() root.mainloop()
Python Tkinter color dialog options:
askcolor
function in Tkinter's colorchooser
module has options for configuring the color picker dialog, such as the initial color.import tkinter as tk from tkinter import colorchooser def pick_color(): initial_color = "#00ff00" # Initial color in hexadecimal format (Green) color = colorchooser.askcolor(title="Choose a color", initialcolor=initial_color)[1] print("Selected color:", color) root = tk.Tk() color_button = tk.Button(root, text="Pick a Color", command=pick_color) color_button.pack() root.mainloop()
Adding a color picker to Tkinter GUI:
import tkinter as tk from tkinter import colorchooser def pick_color(): color = colorchooser.askcolor(title="Choose a color")[1] color_label.config(bg=color) root = tk.Tk() color_label = tk.Label(root, text="Selected color", width=20, height=2) color_button = tk.Button(root, text="Pick a Color", command=pick_color) color_label.pack(pady=10) color_button.pack() root.mainloop()