This file contains the graph object (graph.py)
Code: Select all
# graph.py
# class to show a moving graph
# written by Roger Woollett
from sys import version_info
if version_info[0] < 3:
import Tkinter as tk
else:
import tkinter as tk
class Trace():
# class to represent a single trace in a ScrollGraph
def __init__(self,master,max_x,min_x,colour,size):
self.master = master
self.size = size
self.scale = master.height/(max_x - min_x)
self.offset = -self.scale*min_x
# create a list of line ids
self.lines=[]
i = 0
while i < master.width:
self.lines.append(master.create_line(i,0,i,0,fill = colour,width = size))
i += 1
def scroll(self,value):
# scroll to the right and add new value
value = self.scale*value + self.offset
# we want positive upwards
value = self.master.height - value
# do scroll
i = self.master.width - 1
while i > 0:
x = self.master.coords(self.lines[i - 1])
self.master.coords(self.lines[i],i,x[1],i,x[3])
i -= 1
# add new value
self.master.coords(self.lines[0],0,value,0,value + self.size)
class ScrollGraph(tk.Canvas):
# class to show a scrolling graph
def __init__(self,master,*args,**kwargs):
tk.Canvas.__init__(self,master,*args,**kwargs)
self.width = int(self['width'])
self.height = int(self['height'])
self.traces = {}
def add_trace(self,name,max_x,min_x,colour = 'black',size = 1):
# call to add a trace to the graph
self.traces[name] = Trace(self,max_x,min_x,colour=colour,size = size)
def scroll(self,name,value):
# call to add a new value to a trace
self.traces[name].scroll(value)
Code: Select all
# trygraph.py
# test program to try out the graph class
# written by Roger Woollett
from sys import version_info
if version_info[0] < 3:
import Tkinter as tk
else:
import tkinter as tk
import graph as g
from math import sin,pi
DELAY = 20 # time period for generating dada points
class Mainframe(tk.Frame):
def __init__(self,master,*args,**kwargs):
tk.Frame.__init__(self,master,*args,**kwargs)
# create the scroll graph
self.graph = g.ScrollGraph(self,width = 300,height = 100)
self.graph.grid(row = 0,column = 0)
# add a thick red sine wave trace
self.graph.add_trace('sin',1,-1,'red',size = 3)
self.angle = 0
# add a thin green saw tooth trace
self.graph.add_trace('saw',100,0,'green')
self.saw = 0
self.inc = 1
# add a quit button
tk.Button(self,text = 'Quit',width = 15,command = master.destroy) \
.grid(row = 1,column = 0)
# start the process of adding data to traces
self.dodata()
def dodata(self):
# add data to both traces
self.graph.scroll('sin',sin(self.angle))
self.angle += 0.05
if self.angle >= 2*pi:
self.angle = 0
self.graph.scroll('saw',self.saw)
self.saw += self.inc
if self.saw == 100:
self.inc = -1
if self.saw == 0:
self.inc = 1
# call this function again after DELAY milliseconds
self.after(DELAY,self.dodata)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title('Try Graph')
Mainframe(self).pack()
App().mainloop()
[EDIT] I should have said to put both files in the same directory.