analitics

Pages

Sunday, August 16, 2026

tkinter : test colorchooser script on pydroid 3.

Learn how to use the Python Tkinter colorchooser module to create interactive graphical interfaces. This simple script opens a native color picker dialog and instantly updates your application interface based on user selection.
How the Python script functions:
The colorchooser module is imported to launch the native system palette window.
The askcolor function runs when triggered and waits for user input.
The script extracts the returned HEX color code from the selection.
The main window background and label text update immediately with the chosen color.
This lightweight code provides a simple way to add dynamic customization to desktop applications without installing external libraries.
Let's see the source code:
import tkinter as tk
from tkinter import colorchooser

def choose_color():
    # Opens the color picker dialog and returns a tuple: ((R, G, B), "#HEX")
    color = colorchooser.askcolor(title="Choose a Background Color")
    
    # Check if the user selected a color (didn't click Cancel)
    if color[1]: 
        hex_code = color[1]
        root.config(bg=hex_code)
        label_color.config(text=f"HEX Code: {hex_code}", bg=hex_code)

# Create the main window
root = tk.Tk()
root.title("Tkinter Colorchooser Example")
root.geometry("300x200")

# Button to trigger the color chooser dialog
btn_choose = tk.Button(root, text="Select Color", command=choose_color)
btn_choose.pack(pady=30)

# Label to display the chosen HEX code
label_color = tk.Label(root, text="No color selected", font=("Arial", 12))
label_color.pack(pady=10)

root.mainloop()