【问题标题】:Stop a tkinter function that has a "return"停止具有“返回”的 tkinter 函数
【发布时间】: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


【解决方案1】:

我不知道这是否是唯一的问题,但它肯定是问题的一部分。这段代码并没有像你认为的那样做:

self.after(0, self.get_data(i+1))

以上内容与以下内容相同:

result = self.get_data(i+1)
self.after(0, result)

...所有意图和目的都与此相同:

result = self.get_data(i+1)
result()

当你调用after 时,你必须给它一个callable。此外,您不应给出零值,因为在某些情况下,您将阻止事件循环能够处理事件,例如刷新显示的请求。您至少应该给出 1 的值,最好是更大的值。

使用after 调用带参数的函数的正确方法是这样的:

self.after(1, self.get_data, i+1)

我想你也打算取消对self.get_data的呼叫,所以你需要这样做:

self.after_id = self.after(1, self.get_data, i+1)

...然后将stop修改为:

def stop(self):
    self.after_cancel(self.after_id)

【讨论】:

  • 我会试试这个。对不起,混乱的初学者代码。我刚刚遇到这么多问题,我只是拍了一些东西。而且,如果它似乎有效,我会接受它。我希望我可以通过一个更简单的项目开始学习 python。 :/ 谢谢你的回答!
  • @keakins13:“我只是拍了一些东西。而且,如果它看起来有效,我就去。” - 这不是一个很好的策略。你需要有条不紊,从小事做起,一路向上。您的代码似乎有很多问题,即使对于经验丰富的程序员来说,尝试一次解决所有问题也非常困难。
  • 所以,看起来(我应该知道这一点)它从未经历过 if True 语句。意思是,self.after 从未被触及。当我在返回 i 之前摆脱该语句并将其(与您的编辑一起)放置时,它不会显示在图表上并继续循环(非常缓慢)。即将对代码进行编辑(根据您的建议)。
  • 你有一个很好的观点。我希望我可以回去慢慢开始。但是我得到了一个(在我看来)非常先进的项目和一个截止日期……我几乎没有编程经验。我真的很抱歉草率的代码....:/
猜你喜欢
  • 2015-01-16
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多