Advertisement
here2share

# Tk_linechart.py

Sep 25th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. # Tk_linechart.py
  2.  
  3. from Tkinter import *
  4. from random import randint
  5. import time
  6.  
  7. def value_to_y(val):
  8.         return 550-5*val
  9.  
  10. class Cv(): 0
  11. cv = Cv()
  12. cv.s = 1
  13. cv.x2 = 50
  14. cv.y2 = value_to_y(randint(0,100))
  15.  
  16. def step():
  17.     if cv.s > 22:
  18.         # new frame
  19.         cv.s = 1
  20.         cv.x2 = 50
  21.         canvas.delete('temp') # only delete items tagged as temp
  22.     x1 = cv.x2
  23.     y1 = cv.y2
  24.     cv.x2 = 50  + cv.s * 50
  25.     cv.y2 = value_to_y(randint(0,100))
  26.     canvas.create_line(x1, y1, cv.x2, cv.y2, fill='red', tags='temp', width=3)
  27.     # print(s, x1, y1, x2, y2)
  28.     cv.s = cv.s+1
  29.     canvas.after(300, step)
  30.  
  31. root = Tk()
  32. root.title('simple line chart')
  33.  
  34. canvas = Canvas(root, width=1200, height=600, bg='white') # 0,0 is top left corner
  35. canvas.pack(expand=YES, fill=BOTH)
  36.  
  37. Button(root, text='Quit', command=root.quit).pack()
  38.  
  39. canvas.create_line(50,550,1150,550, width=2) # x-axis
  40. canvas.create_line(50,550,50,50, width=2)    # y-axis
  41.  
  42. # x-axis
  43. for i in range(23):
  44.     x = 50 + (i * 50)
  45.     canvas.create_line(x,550,x,50, width=1, dash=(2,5))
  46.     canvas.create_text(x,550, text='%d'% (10*i), anchor=N)
  47.    
  48. # y-axis
  49. for i in range(11):
  50.     y = 550 - (i * 50)
  51.     canvas.create_line(50,y,1150,y, width=1, dash=(2,5))
  52.     canvas.create_text(40,y, text='%d'% (10*i), anchor=E)
  53.  
  54. canvas.after(300, step)
  55. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement