Tkinter Window with Random Background Color

  • Share this:

Code introduction


This function generates a Tkinter window with a label that has a random background color.


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))
    
    root = tk.Tk()
    label = tk.Label(root, text="Random Color", bg=random_color())
    label.pack()
    root.mainloop()

# JSON Explanation                
              
Tags: