【发布时间】:2017-06-03 15:31:05
【问题描述】:
我的代码目前正在绘制和写入图形数据的文本文件。我希望它停止这样做。它不是停止,而是重新开始这个过程。我尝试了几件事(停止 get_data 函数、停止 update_graph 函数、具有 单独停止 函数)。我错过了什么?
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)
【问题讨论】:
-
能否请您提供该问题的minimal reproducible example。 IE。当我复制并粘贴代码并运行它时,我需要能够看到问题。
-
我可以粘贴/附加所有的python代码(我现在在学校,树莓派在家里?)你也需要数据的文本文件吗?
-
“文本”文件示例下显示的值通常在 100 到 1000 之间。
-
这看起来代码太多了。您真的需要所有代码来说明问题吗?请阅读如何创建minimal reproducible example
标签: python python-3.x function tkinter