Advertisement
here2share

# Tk_pendulums.py

Mar 21st, 2021
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. # Tk_pendulums.py
  2.  
  3. import random
  4. import math
  5. import colorsys
  6.  
  7. from Tkinter import *
  8.  
  9. def colorT(c):
  10.     return '#%02x%02x%02x' % tuple(c)
  11.  
  12. class pendulum:
  13.     def __init__(self,d,alpha,x0,y0,c):
  14.         self.fix_x = x0
  15.         self.fix_y = y0
  16.         self.length = d
  17.         self.angle = [alpha, alpha]
  18.         self.pos = [0,0]
  19.         self.getpos()
  20.  
  21.         self.m=1000.
  22.         self.g=9.81
  23.         self.damp = 0.999
  24.         self.dt=0.01
  25.         self.color=colorT(c)
  26.         self.cv_dot=None
  27.         self.cv_lin=None
  28.  
  29.     def getpos(self):
  30.         self.pos[0] =  self.length*math.sin(self.angle[0])+self.fix_x
  31.         self.pos[1] =  self.length*math.cos(self.angle[0]) +self.fix_y
  32.  
  33.     def newAngle(self):
  34.         self.angle[1] +=  self.dt *(-self.m*self.g/self.length*math.sin(self.angle[0]))
  35.         self.angle[0] += self.angle[1]*self.dt
  36.  
  37. class MyApp(Tk):
  38.     def __init__(self):
  39.         self.nbi=0
  40.  
  41.  
  42.         Tk.__init__(self)
  43.         fr = Frame(self)
  44.         fr.pack()
  45.         width = 1288
  46.         height = 728
  47.         self.canvas  = Canvas(fr, height = height, width = width,bg= 'black')
  48.         self.canvas.pack()
  49.  
  50.         self.fix = (width/2,0)
  51.         self.p=[]
  52.         l=600
  53.         m=20#m=30
  54.         c=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
  55.         self.r=8
  56.         for i in range(40):
  57.             self.p.append(pendulum(l,-math.pi/4,1280/2,0,c))
  58.             l-=m
  59.             m*=0.96#0.95
  60.  
  61.  
  62.         for i in range(len(self.p)):
  63.             self.p[i].cv_lin =self.canvas.create_line(self.fix[0], self.fix[1],self.p[i].pos[0], self.p[i].pos[1], width=1, fill=colorT((100,100,100)))
  64.             self.p[i].cv_dot =self.canvas.create_oval(self.p[i].pos[0]-self.r, self.p[i].pos[1]-self.r,self.p[i].pos[0]+self.r, self.p[i].pos[1]+self.r )
  65.             self.canvas.itemconfig(self.p[i].cv_dot, outline=self.p[i].color)
  66.             self.canvas.itemconfig(self.p[i].cv_dot, fill=self.p[i].color)
  67.         self.update()
  68.  
  69.     def update(self ):
  70.         h=colorsys.hsv_to_rgb((self.nbi)/1000., 1., 1.)
  71.         c =colorT((h[0]*255,h[1]*255,h[2]*255))
  72.  
  73.         for p_ in self.p:
  74.             p_.newAngle()
  75.             p_.getpos()
  76.             p_.color =c
  77.             self.canvas.itemconfig(p_.cv_dot, outline=p_.color)
  78.             self.canvas.itemconfig(p_.cv_dot, fill=p_.color)
  79.             # self.canvas.itemconfig(p_.cv_lin, fill=p_.color)
  80.             self.canvas.coords(p_.cv_dot,p_.pos[0]-self.r, p_.pos[1]-self.r,p_.pos[0]+self.r, p_.pos[1]+self.r )
  81.             self.canvas.coords(p_.cv_lin, self.fix[0], self.fix[1],p_.pos[0], p_.pos[1])
  82.  
  83.         self.canvas.update()
  84.         self.nbi+=1
  85.  
  86.         return
  87. if __name__ == "__main__":
  88.     root = MyApp()
  89.     i=0
  90.     while 1:
  91.         root.update( )
  92.         root.after(50)
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement