我的代码目前正在绘制和编写图形数据的文本文件。我要它停止这样做。它不是停止,而是重新开始这个过程。我试过几种方法(stoppingget\u data函数,stoppingupdate\u graph函数,有一个单独的stop函数)。我错过了什么?你知道吗
def on_click(self):
if self.ani is None:
# animation is not running; start it
return self.start()
def start(self):
self.points = int(tkvar.get()) + 1
self.ani = animation.FuncAnimation(
self.fig,
self.update_graph,
frames=self.points,
interval=int(self.interval.get()),
repeat=True)
self.running = True
self.btn.config(text='Started')
self.ani._start()
print('started animation')
def get_data(self, i):
files = tkvar.get()
global after_id
global filenumber
global tkvars
filenumber = int(files)
inttime = tkvars.get()
thetime = inttime.encode()
ser.write(thetime)
f = open("Serial" + str(i) + ".txt", 'w+')#encoding='utf-8')
#f = open("serial%d.txt") % (i)
data=ser.readline() #read the defined serial import
f.write(str(data)) #place data into the text file
f.close() #close the text file
lines = open("Serial" + str(i) + ".txt", 'r+')#, encoding='utf-8') #open that same text file (reading priviledges)
read_serial=lines.readline() #read the text file
global mylist
mylist = [int(x) for x in read_serial.split(',') if x.strip().isdigit()] #make the read data an interger and strip it of any data that is not a number (comma, ending/beginning letters)
global x
x = np.linspace(340, 850, num=len(mylist)) #creation of x axis
ax1.clear()
lines.close() #close the text file
ax1.plot(x, mylist)
plt.ylim([0, 1000])
print(filenumber)
print(i)
return i
if True:
self.after_id = self.after(1, self.get_data, i+1)
def update_graph(self, i):
self.get_data(i)
global mylist
global x
global filenumber
self.line.set_data(x, mylist)#(self.get_data()) # update graph
if i >= self.points + 1:
#root.update()
self.btn.config(text='Start')
self.stop()
self.running = False
self.ani = None
return self.line,
def stop(self):
self.after_cancel(self.after_id)
我不知道这是否是唯一的问题,但它肯定是问题的一部分。此代码不会执行您认为的操作:
以上内容与以下内容相同:
。。。就所有意图和目的而言,与此相同:
调用
after
时,必须给它一个可调用的。另外,您不应该将值设为零,因为在某些情况下,您将阻止事件循环处理事件,例如刷新显示的请求。你至少应该给出一个1的值,最好是更大一点的值。你知道吗使用
after
调用带有参数的函数的正确方法如下:我假定您也希望能够取消对
self.get_data
的调用,因此需要执行以下操作:。。。然后将
stop
修改为:相关问题 更多 >
编程相关推荐