Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_canvas_swap_polygons.py
- from tkinter import *
- from math import *
- from time import *
- root = Tk()
- canvas = Canvas(root, width=500, height=500)
- canvas.pack()
- nine_pointed_star = []
- for xy in range(0, 18, 2):
- nine_pointed_star.append(250+240*sin(xy*pi/9))
- nine_pointed_star.append(250-240*cos(xy*pi/9))
- nine_pointed_star.append(250+90*sin((xy+1)*pi/9))
- nine_pointed_star.append(250-90*cos((xy+1)*pi/9))
- canvas.create_polygon(nine_pointed_star, fill='purple', tags='star')
- star = []
- for xy in range(0, 10, 2):
- star.append(250+240*sin(xy*pi/5))
- star.append(250-240*cos(xy*pi/5))
- star.append(250+90*sin((xy+1)*pi/5))
- star.append(250-90*cos((xy+1)*pi/5))
- # swap stars with canvas configure when clicked
- def swap_stars(e):
- if canvas.itemcget('star', 'fill') == 'purple':
- canvas.itemconfig('star', fill='yellow')
- canvas.coords('star', star)
- canvas.update()
- sleep(1)
- canvas.itemconfig('star', fill='purple')
- canvas.coords('star', nine_pointed_star)
- canvas.update()
- canvas.tag_bind('star', '<Button-1>', swap_stars)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement