How to Create Color Picker with tkinter Library in Python


In this example, we will create a button that opens a [Color] dialog box when you click the button.
The colorchooser module is used to open the “Color” dialog box.

(1) skcolor (color=None): Open a “Color” dialog directly, without the parent control and show() method. The return value is a tuple of the form ((R, G, B), “#rrggbb”).

(2) Chooser (master=None): Open a “Color” dialog box. The return value is a tuple of the form ((R, G, B), “#rrggbb”).

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# colorPicker.py

from tkinter import *
import tkinter.colorchooser, tkinter.messagebox

win = Tk()
win.title(string = "Color Dialog")

# Open a color dialog box
def openColorDialog():
    # display color dialog box
    color = colorDialog.show()
    # show the chosen RBG value
    tkinter.messagebox.showinfo("Notice", "The color you chose is: " + color[1] + "n" + 
        "R = " + str(color[0][0]) + " G = " + str(color[0][1]) + " B = " + str(color[0][2]))
        
Button(win, text="Open color dialog box", command=openColorDialog).pack(side=LEFT)

# create a color dialog
colorDialog = tkinter.colorchooser.Chooser(win)

win.mainloop()

When clicking the button, it will open a color dialog box:

When choosing a color, it will show the RGB value of the chosen color:

Subscribe
Notify of
guest
1 Comment
Inline Feedbacks
View all comments
Clément
Clément
3 years ago

Thanks for this quick and efficient answer