Python tkinter module provides an easy way to create one color choosing dialog. We can use its tkinter.colorchooser module. This post will show you how to create a color chooser dialog and how to read the color chosen by the user.
tkinter.colorchooser module provides a method called askColor that can be used to create a color choosing dialog. This method is defined as below:
This method creates a color chooser window, waits for the user to make a selection and once a selection is made, it returns that value.
It can return the selected color value or None.
If the argument color is defined, it will show the color chooser with that color.
Note that the color chooser window will look different on different operating systems.
Let’s tryt his with an example:
from tkinter import * from tkinter import colorchooser def get_color(): selected_color = colorchooser.askcolor(title="Select a color") print(selected_color) window = Tk() button = Button(window, text="Click me", command=get_color) button.pack() window.geometry("500x500") window.mainloop()
- We created one tkinter window.
- We created one button with a text. On clicking this button, it will call the get_color method.
- The button is added to the windows and it will open a 500x500 window.
We can set a default color to this color chooser window. For that, we need to pass the color parameter:
selected_color = colorchooser.askcolor(color="#ffffff", title="Select a color")
If you run this program, it will print the selected color on the console as like below:
((255.99609375, 65.25390625, 60.234375), '#ff413c')
- How to calculate gross pay for hourly paid employees in Python
- Python program to find the volume of a tetrahedron
- Python program to search for a text in a file
- How to insert an item to the start of an Ordered dictionary in Python
- How to write a simple stopwatch in python
- Python program to check if two sets are equal