GUI with autogenerated triangles

I’ve created a Tkinter script that generates random triangles. And of course I am sharing that with you.

tkinter generate triangle

Autogenerated triangle

from tkinter import *
import random

def generate_triangles(count, height, width):
    colors = ['black', 'violet', 'brown', 'orange', 'yellow', 'green', 'red', 'silver', 'gold', 'pink']
    for i in range(int(count)):
        color = random.choice(colors)
        x1 = random.randrange(0, int(width))
        y1 = random.randrange(0, int(height))
        x2 = random.randrange(0, int(width))
        y2 = random.randrange(0, int(height))
        x3 = random.randrange(0, int(width))
        y3 = random.randrange(0, int(height))
        triangle = canvas.create_polygon(x1, y1, x2, y2, x3, y3, fill=color, outline='black')

root = Tk()

canvas = Canvas(root, width=400, height=400)
canvas.pack()
generate_triangles(1, 400, 400)

root.mainloop()

The script generates random triangles using the generate_triangles function. To generate more triangles, adjust the count parameter in the generate_triangles function call. Feel free to customize the colors array as well.

See also  Creating a Multi-Select Drop-Down List in Tkinter