程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了停止执行连续函数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决停止执行连续函数?

开发过程中遇到停止执行连续函数的问题如何解决?下面主要结合日常开发的经验,给出你关于停止执行连续函数的解决方法建议,希望对你解决停止执行连续函数有所启发或帮助;

在我的 tkinter 程序中,我计划有一个按钮来读取串行数据。 这将开始执行一个名为 readserial() 的函数,该函数将抓取数据并将它们显示到文本框中。

这是函数:

#this function reads the incoming data and inserts them into a text frame
def readserial():
    ser_bytes = ser.readline()
    ser_bytes = ser_bytes.decode("utf-8")
    text.insert("end",ser_bytes)
    if vsb.get()[1]==1.0:
       text.see("end")
    root.after(100,readserial)

我们通过按钮调用它。然后它开始以间隔连续执行 - 在 root.after(100,readserial)

中指定

但是,我的串行设备(arduino)将有其他操作模式。 所以我还会有另一个按钮来命令 arduino 停止说话。

由于 arduino 不会发送任何数据,因此我还必须停止 readserial() 的执行。

可以吗?

解决方法

after() 函数调用可以通过 after_cancel(id) 函数停止。每次调用 after(...) 函数时,它都会返回一个 after id,可用于停止函数调用。你可以参这个answer。

...

after_ids = {}

# this function reads the incoming data and inserts them into a text frame
def readserial():
    ser_bytes = ser.readline()
    ser_bytes = ser_bytes.decode("utf-8")
    text.insert("end",ser_bytes)
    if vsb.get()[1] == 1.0:
        text.see("end")
    after_ids[1] = root.after(100,readserial)


def stopserial():
    if after_ids.get(1) is not None:
        root.after_cancel(after_ids[1])

...

大佬总结

以上是大佬教程为你收集整理的停止执行连续函数全部内容,希望文章能够帮你解决停止执行连续函数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: