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!
# 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()
About the Author