python计时器无法工作两次

2024-04-20 05:31:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Windows7中使用Python2.7。 我用过定时器
但计时器只能运行一次

 import threading 

 class BesDldMainWnd(QMainWindow):

     def fun_timer(self):
          print 1


     def StartAll(self):
          timer = threading.Timer(30, self.fun_timer)         
          timer.start()

m = BesDldMainWnd()
m.Startall()
time.sleep(30)
m.Startall()

我以为计时器会运行两次,但它只运行一次


Tags: importselfdefclass计时器printthreadingtimer
1条回答
网友
1楼 · 发布于 2024-04-20 05:31:12

我试过这个密码

import threading

cnt = 0
def fun_timer():
  global cnt
  cnt = cnt + 1
  print "hello", cnt

timer = threading.Timer(1., fun_timer)         
timer.start()

timer = threading.Timer(2., fun_timer)         
timer.start()

结果如下

hello 1
hello 2

所以它的工作原理和预期一样

相关问题 更多 >