import tkinter as tk
def draw_square(x, y, size, color):
canvas.create_rectangle(x, y, x + size, y + size, fill=color)
def draw_cascade(x, y, size, color):
draw_square(x, y, size, color)
if size > 5:
draw_cascade(x + 10, y + 10, size - 20, color)
root = tk.Tk()
canvas = tk.Canvas(root, width=300, height=300)
canvas.pack()
draw_cascade(10, 10, 280, "red")
draw_cascade(20, 20, 260, "blue")
draw_cascade(30, 30, 240, "green")
draw_cascade(40, 40, 220, "yellow")
draw_cascade(50, 50, 200, "orange")
draw_cascade(60, 60, 180, "purple")
draw_cascade(70, 70, 160, "pink")
draw_cascade(80, 80, 140, "brown")
draw_cascade(90, 90, 120, "black")
draw_cascade(100, 100, 100, "white")
draw_cascade(110, 110, 80, "grey")
draw_cascade(120, 120, 60, "cyan")
draw_cascade(130, 130, 40, "magenta")
draw_cascade(140, 140, 20, "lime")
draw_cascade(150, 150, 0, "maroon")
root.mainloop()
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
import tkinter as tk
def draw_square(x, y, size, color):
canvas.create_rectangle(x, y, x + size, y + size, fill=color)
def draw_cascade(x, y, size, color):
draw_square(x, y, size, color)
if size > 5:
draw_cascade(x + 10, y + 10, size - 20, color)
root = tk.Tk()
canvas = tk.Canvas(root, width=300, height=300)
canvas.pack()
draw_cascade(10, 10, 280, "red")
draw_cascade(20, 20, 260, "blue")
draw_cascade(30, 30, 240, "green")
draw_cascade(40, 40, 220, "yellow")
draw_cascade(50, 50, 200, "orange")
draw_cascade(60, 60, 180, "purple")
draw_cascade(70, 70, 160, "pink")
draw_cascade(80, 80, 140, "brown")
draw_cascade(90, 90, 120, "black")
draw_cascade(100, 100, 100, "white")
draw_cascade(110, 110, 80, "grey")
draw_cascade(120, 120, 60, "cyan")
draw_cascade(130, 130, 40, "magenta")
draw_cascade(140, 140, 20, "lime")
draw_cascade(150, 150, 0, "maroon")
root.mainloop()