开发者

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

开发者 https://www.devze.com 2022-12-22 18:21 出处:网络
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 dependen

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

精彩评论

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