Tkinter Random Color Generator

  • Share this:

Code introduction


This function generates a random color and displays it in a Tkinter window.


Technology Stack : Tkinter, random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import tkinter as tk
from tkinter import messagebox

def generate_random_color():
    def random_color():
        import random
        return "#%06x" % random.randint(0, 0xFFFFFF)

    def apply_random_color():
        root = tk.Tk()
        root.geometry("200x200")
        label = tk.Label(root, text="Random Color", bg=random_color())
        label.pack()
        root.mainloop()

    return apply_random_color                
              
Tags: