gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
write code for a gui with 10 buttons that change color.
- User
write code for a gui with 10 buttons that change color.
- Assistant
Certainly! Below is a simple example of a GUI using Python's
tkinterlibrary that creates a window with 10 buttons. Each button changes color when clicked.You'll need to have Python installed on your system. If you don't have it yet, you can download it from the official Python website.
Here's the code:
import tkinter as tk import random # Function to change color of the button def change_color(button): # Generate a random color in hex format random_color = "#{:06x}".format(random.randint(0, 0xFFFFFF)) button.config(bg=random_color) # Create the main application window root = tk.Tk() root.title("Color Changing Buttons") # Create 10 buttons buttons = [] for i in range(10): button = tk.Button(root, text=f"Button {i+1}", command=lambda btn=button: change_color(btn)) button.pack(pady=5, padx=5, fill=tk.X) buttons.append(button) # Start the main event loop root.mainloop()