You can download this code by clicking the button below.
This code is now available for download.
This function creates a Tkinter window with a button. Each time the button is clicked, the button's background color changes to a random color.
Technology Stack : Tkinter, random
Code Type : Function
Code Difficulty : Intermediate
import tkinter as tk
import random
def random_color_button():
def generate_random_color():
return "#{:06x}".format(random.randint(0, 0xFFFFFF))
root = tk.Tk()
root.title("Random Color Button")
def on_button_click():
current_color = generate_random_color()
color_button.config(bg=current_color)
color_button = tk.Button(root, text="Click for Random Color", command=on_button_click)
color_button.pack(pady=20)
root.mainloop()