Advertisement
here2share

# Tk_ani_ribbons.py

Dec 8th, 2020
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. # Tk_ani_ribbons.py
  2.  
  3. from math import cos,sin,pi,radians
  4. import time
  5. import random
  6.  
  7. r=random.randint
  8. now=time.time
  9.  
  10. try:
  11.     import tkinter as tk
  12. except:
  13.     import Tkinter as tk
  14.  
  15. root=tk.Tk()  
  16. root.title("Tk Animating Ribbons")
  17.  
  18. go=0
  19.  
  20. ww=640
  21. hh=640
  22.  
  23. canvas=tk.Canvas(root,width=ww,height=hh)
  24. canvas.pack(fill="both",expand=True)
  25.  
  26. do360={}
  27. for angle in range(0,360):
  28.     a=cos(radians(angle))
  29.     b=sin(radians(angle))
  30.     do360[angle]=(a,b)
  31. def i360(angle):
  32.     return do360[angle%360]
  33.    
  34. # for fluidity
  35. wave={}
  36. sp={}
  37. def WAVE(span):
  38.     global polar
  39.     polar *= -1
  40.     incr=360.0/span
  41.     for angle in range(0,span):
  42.         i=cos(radians((angle*incr)*polar))
  43.         yield i
  44.  
  45. colors='darkorange blue'.split()
  46. marks={}
  47.  
  48. def make_line(i,z):
  49.     x0,y0,x1,y1=(i360(z)+i360(z))
  50.     x0*=r0
  51.     y0*=r0
  52.     x1*=r1
  53.     y1*=r1
  54.     t=x+xm+x0,y+ym+y0,x+xm+x0+x1,y+ym+y0+y1
  55.     if go:
  56.         t=canvas.create_line(t,width=2,fill=colors[i])
  57.     marks[i].append(t)
  58.  
  59. xm,ym=ww/2,hh/2
  60. L=800
  61. for i in (0,1):
  62.     x=y=0
  63.     marks[i]=[]
  64.     r0=100
  65.     r1=300
  66.     marks[i,'z']=r(0,359)
  67.     for z in range(0,L,4):
  68.         make_line(i,z)
  69.    
  70. def get_wave(span):
  71.     global wave
  72.     while 1:
  73.         try:
  74.             zzz=next(wave)*span/5.0
  75.             break
  76.         except:
  77.             wave=WAVE(span)
  78.     return zzz
  79.  
  80. go=1
  81. polar = -1
  82. rnd_rotate = 180
  83. while go:
  84.     time=now()+0.05
  85.     for i in [0]:
  86.         canvas.delete(marks[i][0])
  87.         marks[i] = marks[i][-L:]
  88.         z=marks[i,'z']
  89.         x=get_wave(600)
  90.         y=get_wave(500)
  91.         r0=get_wave(300)+100
  92.         r1=get_wave(200)+300
  93.         z += 1
  94.         z %= 360
  95.         marks[i,'z']=z
  96.         if z == rnd_rotate:
  97.             polar = random.choice((1,-1))
  98.             rnd_rotate = (r(20,340)+z)%360
  99.         canvas.update()
  100.         make_line(i,z)
  101.     while time > now():
  102.         0
  103.        
  104. tk.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement