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 generates a random color when clicked.
Technology Stack : tkinter, random
Code Type : Python Function
Code Difficulty : Intermediate
import tkinter as tk
import random
def random_color():
return "#{:06x}".format(random.randint(0, 0xFFFFFF))
def create_button(root, text, command):
color = random_color()
button = tk.Button(root, text=text, bg=color, command=command)
button.pack(pady=20)
def generate_random_color():
return random_color()
def main():
root = tk.Tk()
root.title("Random Color Button")
create_button(root, "Generate Color", generate_random_color)
root.mainloop()
# Usage example:
# main()