Random Color Button Creation Script

  • Share this:

Code introduction


This code defines a function to generate a random color button. It imports the Tkinter library, defines a function to generate a random color, then creates a button with a random background color. Clicking the button will close the window.


Technology Stack : The code uses the Tkinter library and technology stack.

Code Type : Function

Code Difficulty :


                
                    
import tkinter as tk
import random

def generate_random_color():
    return f'#{random.randint(0, 0xFFFFFF):06x}'

def create_color_button(root, color):
    button = tk.Button(root, text=f"Color: {color}", bg=color, command=root.destroy)
    button.pack(pady=10)

def random_color_button_generator():
    root = tk.Tk()
    root.title("Random Color Button Generator")
    color = generate_random_color()
    create_color_button(root, color)
    root.mainloop()
    return root