python定时器(tkinter)

之前使用Tkinter模块时,对于定时器的需求,使用模块中的after,但是随着时间的需求,譬如,最近需要定时20ms调用一个函数,发现after总是在接近40ms的时候才调用。

在此记录一种较为精确的定时器。

class threading.Timer(interval, function, args=[], kwargs={})

创建一个timer,在interval秒过去之后,它将以参数args和关键字参数kwargs运行function 。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

from threading import Timer

class test:

    def __init__(self):

        self.t = Timer(0.02self.func)

        self.t.start()

    def func(self):

        print 'hello'

        self.t.cancel()

        self.t = Timer(0.02self.func)

        self.t.start()

             

= test()

转载地址:https://www.cnblogs.com/pinking/p/6897292.html 

猜你喜欢

转载自blog.csdn.net/whalefall/article/details/87874696