本文介绍了Python中的Backgroundworker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一个没有经验的python程序员.
I'm an inexperienced python programmer.
是否可以使用backgroundworker,使其在程序启动时启动,并在程序关闭时关闭?
Is there a way to use the backgroundworker so that it starts at program startup and closes when program close?
我希望它观看一个按钮,按下该按钮将返回1.因此,当程序运行时只要button = 1 button都必须执行"this"操作.
I want it to watch a button, the button returns 1 when pressed. So while the program in running whenever button = 1 button has to do "this".
有人可以帮我吗?
推荐答案
有意义的是在您的主程序中启动一个单独的线程,并在后台执行任何操作.作为示例,请检查以下相当简单的代码:
Would make sense to start a separate thread within your main program and do anything in the background. As an example check the fairly simple code below:
import threading
import time
#Routine that processes whatever you want as background
def YourLedRoutine():
while 1:
print 'tick'
time.sleep(1)
t1 = threading.Thread(target=YourLedRoutine)
#Background thread will finish with the main program
t1.setDaemon(True)
#Start YourLedRoutine() in a separate thread
t1.start()
#You main program imitated by sleep
time.sleep(5)
这篇关于Python中的Backgroundworker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!