首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python-3运行While循环X个时间段

Python-3运行While循环X个时间段
EN

Stack Overflow用户
提问于 2019-06-18 05:33:14
回答 1查看 79关注 0票数 0

我的程序是为一个raspberry pi项目编写的,在这个项目中,我为用户提供了当他们按下按钮时录制消息的选项。我目前的目标是让一个while循环通过按下按钮然后退出循环来提供录制的选项,如果没有按下它5秒,它就会退出循环。

下面是我的代码:

代码语言:javascript
复制
w = True
while w:
    # if the button on the pi is pressed once this while loop begins,
    if GPIO.input(17) == 0:
        print ("Button Pressed")
        sleep(2)
        print ("Explaining recording instructions")
        pygame.mixer.Sound.play(record_s)
        sleep(8)
        print ("Recording")
        record_audio()
        # Exit the loop once message is recorded by pressing the button once more
        if GPIO.input(17) == 0:
            w = False
    # If the button is not pressed for 5 seconds,
    elif sleep(5):
        print ("No input")
        # Exit the loop
        w= False

我尝试了几种不同的方法,做了大量的谷歌搜索,并查看了类似的问题,但都没有奏效。

EN

回答 1

Stack Overflow用户

发布于 2019-06-18 05:48:09

代码语言:javascript
复制
def run():
    while True:
    # if the button on the pi is pressed once this while loop begins,
        if GPIO.input(17) == 0:
            print ("Button Pressed")

        print ("Explaining recording instructions")
        pygame.mixer.Sound.play(record_s)
        sleep(8)
        print ("Recording")
        record_audio()
        # Exit the loop once message is recorded by pressing the button once more
        if GPIO.input(17) == 0:
            break

def main():
    t = threading.Thread(target=run)
    t.daemon = True
    t.start()
    time.sleep(5)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56639171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档