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 that changes its background color randomly every time it is clicked.
Technology Stack : Tkinter
Code Type : Tkinter GUI Application
Code Difficulty : Intermediate
import tkinter as tk
import random
def generate_random_color():
def random_color():
return "#{:06x}".format(random.randint(0, 0xFFFFFF))
def color_button():
color = random_color()
return tk.Button(text=f"Color: {color}", bg=color, command=color_button)
root = tk.Tk()
root.title("Random Color Generator")
button = color_button()
button.pack()
root.mainloop()