graph.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import tkinter as tk
  2. import time, math, noise
  3. from PIL import Image, ImageDraw, ImageTk
  4. class Graph(tk.Canvas):
  5. def __init__(self, root, **kwargs):
  6. self.root = root
  7. tk.Canvas.__init__(self, root, **kwargs)
  8. self.height = self.winfo_reqheight()
  9. self.width = self.winfo_reqwidth()
  10. self.coord = [(10, 50), (20, 50), (100, 100)]
  11. self.drawBackground()
  12. self.canvas = self.bg.copy()
  13. self.image = self.create_image(0, 0, image=None, anchor='nw')
  14. self.bind("<Configure>", self.on_resize)
  15. self.n = 0
  16. def drawBackground(self):
  17. self.bg = Image.new('RGB', (self.width, self.height), (0,20,0))
  18. draw = ImageDraw.Draw(self.bg)
  19. draw.line([(0, self.height/2), (self.width, self.height/2)], (0,127,127), 1)
  20. draw.line([(self.width/2, 0), (self.width/2, self.height)], (0,127,127), 1)
  21. def on_resize(self,event):
  22. # determine the ratio of old width/height to new width/height
  23. self.width = max(400, event.width-4)
  24. self.height = max(400, event.height-4)
  25. # resize the canvas
  26. self.config(width=self.width, height=self.height)
  27. self.canvas = self.canvas.resize((self.width, self.height))
  28. self.drawBackground()
  29. self.canvas = Image.blend(self.canvas, self.bg, 0.1)
  30. def update(self):
  31. self.coord = [self.coord[-1]]
  32. for i in range(30):
  33. self.coord.append(((noise.pnoise1(self.n)+1)*self.width/2, (noise.pnoise1(self.n*1.3+3)+1)*self.height/2))
  34. self.n += 0.001
  35. self.canvas = Image.blend(self.canvas, self.bg, 1/250)
  36. draw = ImageDraw.Draw(self.canvas)
  37. draw.line(self.coord, fill=(100, 255, 100, 255), width=int(self.height/100), joint='curve')
  38. self.photo = ImageTk.PhotoImage(self.canvas)
  39. self.itemconfig(self.image, image=self.photo)