开发者

Is there an alternative to gobject.timeout_add() in Python?

I'm using gobject.timeout_add() to display a timer in my project (dojotools).

I was wondering if there is 开发者_如何学运维any other way to achieve the same results, but without the gobject dependency.

The requirement is just calling a method repeatedly, in a certain time interval.


You can use a thread with time.sleep(interval) to achieve the same effect.

import thread, time

def timer():
    while True:
        time.sleep(1)
        # do something here

thread.start_new_thread(timer, ())


If you are OK with using TkInter, which should be available on all Pythons (except cell phones and maybe some tablets), you can use TkInter's after() function.
That would avoid the blocking that sleep causes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜