Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_linechart.py
- from Tkinter import *
- from random import randint
- import time
- def value_to_y(val):
- return 550-5*val
- class Cv(): 0
- cv = Cv()
- cv.s = 1
- cv.x2 = 50
- cv.y2 = value_to_y(randint(0,100))
- def step():
- if cv.s > 22:
- # new frame
- cv.s = 1
- cv.x2 = 50
- canvas.delete('temp') # only delete items tagged as temp
- x1 = cv.x2
- y1 = cv.y2
- cv.x2 = 50 + cv.s * 50
- cv.y2 = value_to_y(randint(0,100))
- canvas.create_line(x1, y1, cv.x2, cv.y2, fill='red', tags='temp', width=3)
- # print(s, x1, y1, x2, y2)
- cv.s = cv.s+1
- canvas.after(300, step)
- root = Tk()
- root.title('simple line chart')
- canvas = Canvas(root, width=1200, height=600, bg='white') # 0,0 is top left corner
- canvas.pack(expand=YES, fill=BOTH)
- Button(root, text='Quit', command=root.quit).pack()
- canvas.create_line(50,550,1150,550, width=2) # x-axis
- canvas.create_line(50,550,50,50, width=2) # y-axis
- # x-axis
- for i in range(23):
- x = 50 + (i * 50)
- canvas.create_line(x,550,x,50, width=1, dash=(2,5))
- canvas.create_text(x,550, text='%d'% (10*i), anchor=N)
- # y-axis
- for i in range(11):
- y = 550 - (i * 50)
- canvas.create_line(50,y,1150,y, width=1, dash=(2,5))
- canvas.create_text(40,y, text='%d'% (10*i), anchor=E)
- canvas.after(300, step)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement