简体   繁体   English

按下tkinter上的一个按钮时,如何中断睡眠

[英]How can I break the sleep when I pressed a button on tkinter

I have a sensor() method, and I use root.after(200, sensor) to run it all the time. 我有一个sensor()方法,并且我root.after(200, sensor)使用root.after(200, sensor)来运行它。 I also have a function set_speed() in it which can set the speed of motor. 我也有一个函数set_speed() ,它可以设置电动机的速度。 When the sensor detects something, it will run 当传感器检测到某物时,它将运行

set_speed(100)
sleep(3)
set_speed(0)

Now I need to add another button, when the sensor detects something, we can press the button the break the sleep (bu the sensor is still running). 现在,我需要添加另一个按钮,当传感器检测到某些东西时,我们可以按一下按钮以中断睡眠(但传感器仍在运行)。

It seems that I need to add another root.after to create another thread or something, I have tried for a while but was not able to complete it. 似乎我需要添加另一个root。创建另一个线程之类的东西之后,我尝试了一段时间,但无法完成它。

Problem solved. 问题解决了。 We can simply use another after to build another thread which will be run 3 seconds later and set the speed as 0. 我们可以简单地使用另一个来构建另一个线程,该线程将在3秒后运行并将速度设置为0。

    set_speed(100)
    root.after(3000, stop)   

.....

def stop():
    set_speed(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM