Advertisement
here2share

# Tk_linechart2.py

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