我使用tkinter和pyhton2.7构建了一个GUI,我已经成功地在我的GUI中绘制来自我的串口的数据(使用Matplotlib)。我的代码快照如下:
""All required import was done here""
x = []
adc_data = []
q = [0]
f = plt.Figure(figsize = (9,5), dpi = 100)
ax = f.add_subplot(111)
class ADC_Ref_Data(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_geometry(self, '900x600+200+150')
tk.Tk.wm_title(self, "ADC Referene")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
frame = StartPage(container, self)
self.frames[StartPage] = frame
frame.grid(row=0, column=0, sticky = "nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
self.button4 = ttk.Button(self, text="Start",
command=lambda: self.start())
self.button4.place(relx = 0.03, rely = 0.95, height = 30 , width = 80 , anchor = 'sw')
self.canvas = FigureCanvasTkAgg(f, self)
self.canvas.show()
self.canvas.get_tk_widget().place(relx = 0.5, rely = 0.48, relwidth = 1, relheight = 0.8, anchor = 'center' )
def animate(self,i):
while self.button4['text'] == 'Pause':
self.ser.write(str(chr(250)))
data = self.ser.read(1)
data1 = self.ser.read(1)
LSB = ord(data)
MSB = ord(data1)
dec = 256*MSB+LSB
q[0] = q[0]+1
x.append(q[0]) #adding data to list
adc_data.append(dec) #adding data to list
plt.pause(1)
ax.clear()
a = ax.plot(x,adc_data,'^r')
def start(self):
if self.button4['text'] == 'Start':
ani = animation.FuncAnimation(f, self.animate,init_func=self.init,frames = 10, interval=1000)#,blit = True)
f.canvas.show()
app = ADC_Ref_Data()
app.mainloop()
从上面的代码可以看出,每当我按下我的Gui上的Start Button时,它将调用一个函数“Start”,其中调用Funcanimation并连续绘制图形(在无限循环中)。现在,当我按下一个停止按钮时,我想停止这个Funcanimation。
如何停止运行Funcanimation?
请帮我解决这个问题。
提前致谢
答案 0 :(得分:0)
尝试
Undefined variable: name
ani.event_source.stop()