Another exercise in applied geometry. This time we use the Tkinter GUI canvas and its create_line() function to draw a triangle, or a series of connected triangles to create something that looks like fancy art work. You might be able to impress grandmama with that one!

565 Views
``````# draw triangles with Tkinter canvas.create_line()
# creates a fancy geometric design
# tested with Python24    vegaseat    26jun2006

from Tkinter import *
from math import sin, cos
import time

root = Tk()

# set width and height of canvas
w = 600
h = 350
# create the canvas for drawing
c = Canvas(width=w, height=h, bg='yellow')
c.pack()

color_list = ['blue', 'red', 'black']

# accumulate the spokes drawings
for spokes in range(3, 16):
# loop through the color list
color = color_list[spokes % 3]
root.title("Spokes = " + str(spokes))
radians = 360 / (spokes * 57.29578)
for x in range(spokes):
for y in range(spokes):
c.create_line((int)(sin(y * radians) * 225 + 300),(int)(cos(y * radians) * 145 + 170),
(int)(sin(x * radians) * 225 + 300),(int)(cos(x * radians) * 145 + 170), fill=color)
root.update()
# delay each drawing a few seconds
time.sleep(3)

root.mainloop()``````

Scientist

A timely update ...

``````''' tk_draw_triangles1.py
draw triangles with Tkinter canvas.create_line()
creates a fancy geometric design
modified and tested with Python27 and Python33
image is at: http://prntscr.com/rrqi3
'''

from math import sin, cos
import time
try:
# Python2
import Tkinter as tk
except ImportError:
# Python3
import tkinter as tk

root = tk.Tk()
w = 600
h = 350
xp = 100
yp = 70
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("{}x{}+{}+{}".format(w, h, xp, yp))

# create the canvas for drawing
cv = tk.Canvas(width=w, height=h, bg='yellow')
cv.pack()

color_list = ['blue', 'red', 'black']

# accumulate the spokes drawings
for spokes in range(3, 16):
# loop through the color list
color = color_list[spokes % 3]
root.title("Spokes = " + str(spokes))
radians = 360 / (spokes * 57.29578)
for x in range(spokes):
for y in range(spokes):
cv.create_line((int)(sin(y * radians) * 225 + 300),
(int)(cos(y * radians) * 145 + 170),
(int)(sin(x * radians) * 225 + 300),
(int)(cos(x * radians) * 145 + 170),
fill=color)
root.update()
# delay each drawing a few seconds
time.sleep(3)

root.mainloop()
``````