擅长:python、mysql、java
<p>最好的方法是启动计时器线程一次。在你的计时器线程中,你可以编写以下代码</p>
<pre><code>class MyThread(Thread):
def __init__(self, event):
Thread.__init__(self)
self.stopped = event
def run(self):
while not self.stopped.wait(0.5):
print("my thread")
# call a function
</code></pre>
<p>在启动计时器的代码中,然后可以<code>set</code>停止事件来停止计时器。</p>
<pre><code>stopFlag = Event()
thread = MyThread(stopFlag)
thread.start()
# this will stop the timer
stopFlag.set()
</code></pre>