I designed a GUI to plot a graph from the text file. However, the plots will not update when I added new points into the text file. I tried using the after() method to keep looping my function to plot the graph but it fails to work. It would not update my plots and it would not even print out 'Updating...' as shown in the code. Would like to know what went wrong and is there a better way to update my plots? Thanks.
class Timer:
def __init__(self, parent):
self.parent = parent
self.time1 = " "
self.time_label = Label(topFrame, text=self.time1, font = "Helvetica 12 bold")
self.time_label.grid(row = 1, column = 1)
self.random_lbl = Label(topFrame, text="Virtual Microgrid", font = "Helvetica 16 bold italic")
self.random_lbl.grid(row = 0, column = 1)
self.random_lbl = Label(topFrame, text="GRAPHS", font = "Helvetica 12 bold" )
self.random_lbl.grid(row = 2, column = 5)
self.update_clock()
def update_clock(self):
self.time2 = time.strftime("%H:%M:%S")
self.time1 = self.time2
self.time_label.config(text=self.time1)
self.parent.after(200, self.update_clock)
def update(self):
self.animate()
print("Updating.....")
self.parent.after(1, self.update)
def animate():
graph_data = open('example.txt', 'r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x,y = line.split(',')
xs.append(x)
ys.append(y)
graph_data = open('example1.txt', 'r').read()
lines = graph_data.split('\n')
xs1 = []
ys1 = []
for line in lines:
if len(line) > 1:
x,y = line.split(',')
xs1.append(x)
ys1.append(y)
graph_data = open('example2.txt', 'r').read()
lines = graph_data.split('\n')
xs2 = []
ys2 = []
for line in lines:
if len(line) > 1:
x,y = line.split(',')
xs2.append(x)
ys2.append(y)
graph_data = open('example3.txt', 'r').read()
lines = graph_data.split('\n')
xs3 = []
ys3 = []
for line in lines:
if len(line) > 1:
x,y = line.split(',')
xs3.append(x)
ys3.append(y)
graph_data = open('example4.txt', 'r').read()
lines = graph_data.split('\n')
xs4 = []
ys4 = []
for line in lines:
if len(line) > 1:
x,y = line.split(',')
xs4.append(x)
ys4.append(y)
fig = Figure(figsize=(13,3.25))
a = fig.add_subplot(111)
a.plot(xs,ys,color='red')
a.plot(xs1,ys1,color='blue')
a.plot(xs4,ys4,color='black')
a.set_title ("SOURCE", fontsize=8)
a.set_ylabel("Voltage/V", fontsize=8)
a.set_xlabel("Time/s", fontsize=8)
a.grid()
fig5 = Figure(figsize=(13,3.25))
f = fig5.add_subplot(111)
f.plot(xs2,ys2,color='red')
f.plot(xs3,ys3,color='blue')
f.set_title ("LOAD", fontsize=8)
f.set_ylabel("Voltage/V", fontsize=8)
f.set_xlabel("Time/s", fontsize=8)
f.grid()
canvas = FigureCanvasTkAgg(fig, master=rightFrame)
canvas.get_tk_widget().grid(row = 2, column = 1, sticky = "nswe")
canvas.draw()
canvas5 = FigureCanvasTkAgg(fig5, master=rightFrame)
canvas5.get_tk_widget().grid(row = 3, column = 1, sticky = "nswe")
canvas5.draw()