Tkinter Triangle Art (Python)

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
vegaseat vegaseat is offline Offline Jun 27th, 2006, 1:58 am |
0
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!
Quick reply to this message  
Python Syntax
  1. # draw triangles with Tkinter canvas.create_line()
  2. # creates a fancy geometric design
  3. # tested with Python24 vegaseat 26jun2006
  4.  
  5. from Tkinter import *
  6. from math import sin, cos
  7. import time
  8.  
  9. root = Tk()
  10.  
  11. # set width and height of canvas
  12. w = 600
  13. h = 350
  14. # create the canvas for drawing
  15. c = Canvas(width=w, height=h, bg='yellow')
  16. c.pack()
  17.  
  18. color_list = ['blue', 'red', 'black']
  19.  
  20. # accumulate the spokes drawings
  21. for spokes in range(3, 16):
  22. # loop through the color list
  23. color = color_list[spokes % 3]
  24. root.title("Spokes = " + str(spokes))
  25. radians = 360 / (spokes * 57.29578)
  26. for x in range(spokes):
  27. for y in range(spokes):
  28. c.create_line((int)(sin(y * radians) * 225 + 300),(int)(cos(y * radians) * 145 + 170),
  29. (int)(sin(x * radians) * 225 + 300),(int)(cos(x * radians) * 145 + 170), fill=color)
  30. root.update()
  31. # delay each drawing a few seconds
  32. time.sleep(3)
  33.  
  34. root.mainloop()

Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC