Advertisement
here2share

# Tk_canvas_swap_polygons.py

Aug 11th, 2022
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. # Tk_canvas_swap_polygons.py
  2.  
  3. from tkinter import *
  4. from math import *
  5. from time import *
  6.  
  7. root = Tk()
  8.  
  9. canvas = Canvas(root, width=500, height=500)
  10. canvas.pack()
  11.  
  12. nine_pointed_star = []
  13. for xy in range(0, 18, 2):
  14.     nine_pointed_star.append(250+240*sin(xy*pi/9))
  15.     nine_pointed_star.append(250-240*cos(xy*pi/9))
  16.     nine_pointed_star.append(250+90*sin((xy+1)*pi/9))
  17.     nine_pointed_star.append(250-90*cos((xy+1)*pi/9))
  18. canvas.create_polygon(nine_pointed_star, fill='purple', tags='star')
  19.  
  20. star = []
  21. for xy in range(0, 10, 2):
  22.     star.append(250+240*sin(xy*pi/5))
  23.     star.append(250-240*cos(xy*pi/5))
  24.     star.append(250+90*sin((xy+1)*pi/5))
  25.     star.append(250-90*cos((xy+1)*pi/5))
  26.  
  27. # swap stars with canvas configure when clicked
  28. def swap_stars(e):
  29.     if canvas.itemcget('star', 'fill') == 'purple':
  30.         canvas.itemconfig('star', fill='yellow')
  31.         canvas.coords('star', star)
  32.         canvas.update()
  33.         sleep(1)
  34.         canvas.itemconfig('star', fill='purple')
  35.         canvas.coords('star', nine_pointed_star)
  36.         canvas.update()
  37.  
  38. canvas.tag_bind('star', '<Button-1>', swap_stars)
  39.  
  40. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement