You can download this code by clicking the button below.
This code is now available for download.
This code creates a tkinter window with a button that changes color randomly. When the button is clicked, it prints a message. The code demonstrates basic usage of tkinter, including window creation, button creation, and event binding.
Technology Stack : tkinter
Code Type : The type of code
Code Difficulty :
import tkinter as tk
import random
def create_random_button(root, text, command):
# Create a random color for the button
color = "#%06x" % random.randint(0, 0xFFFFFF)
# Create a button with the random color and text
button = tk.Button(root, text=text, bg=color, command=command)
button.pack()
# Function to be called when the button is clicked
def button_click():
print("Button was clicked!")
# Create the main window
root = tk.Tk()
root.title("Random Button Example")
# Create a random button with random text
create_random_button(root, "Click Me", button_click)
# Start the main loop
root.mainloop()