import tkinter from tkinter import Canvas # This function initializes a Canvas and display window, # then calls the recursive function triangle to do all the work def start_triangle(size, color): window = tkinter.Tk() c = Canvas(window, width = size, height = size) c.pack() c.create_rectangle(1, 1, size + 1, size + 1, fill=color) triangle(c, 0, 0, size) # draw a Sierpinski triangle with top left at (x,y) and # with height and width of size def triangle(c, x, y, size): # stop when the size gets smaller than 2 # (since we can't subdivide a pixel) if size < 2: return None half = size // 2 draw_square(c, x, y, half) # recursively repeat the triangle # for each of the other three quadrants triangle(c, , , ) # Fill in the missing parameters of this call triangle(c, , , ) # Fill in the missing parameters of this call triangle(c, , , ) # Fill in the missing parameters of this call def draw_square(c, x, y, half): # draw a square in the upper left quadrant c.create_rectangle(1 + x, 1 + y, 1 + x + half, 1 + y + half, width=0, fill="white")