如何在python中创建计时器

2024-05-13 00:31:47 发布

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

我正在编写python脚本,因为我想每2秒钟设置一次计时器,以便更新setLabel函数中的值。我需要为setLabel创建每个不同值的计时器,因为它们只允许我一次更新一次。你知道吗

例如:

#set the timer for 2 seconds to update the value in the setlabel
self.getControl(4202).setLabel("1%")


#Stop the timer and set the timer again for 2 seconds
self.getControl(4202).setLabel("8%")


#Stop the timer and set the timer again for another 2 seconds
self.getControl(4202).setLabel("16%")

等等。。。你知道吗

您能告诉我如何创建计时器,以便一次更新每个计时器中的值吗?你知道吗

编辑:当我尝试此操作时:

# Get the loaded data
                         for channel in tv_elem.findall('channel'):
                             channel_name = 
channel.find('display-name').text
                             for program in channel.findall('programme'):
                                 title = program.find('title').text
                                 start_time = program.get("start")
                                 stop_time = program.get("stop")
                                 cur.execute("INSERT INTO programs(channel, 
title, start_date, stop_date)" + " VALUES(?, ?, ?, ?)", [channel_name, 
title, start_time, stop_time])
                                 con.commit()
                                 con.close

                                 time.sleep(2)
                                 #Stop the timer and set the timer again 
for 2 seconds
                                 self.getControl(4202).setLabel("8%")

                                 time.sleep(2)
                                 #Stop the timer and set the timer again 
for another 2 seconds
                                 self.getControl(4202).setLabel("16%")

                                 time.sleep(2)
                                 #Stop the timer and set the timer again 
for another 2 seconds
                                 self.getControl(4202).setLabel("24%")

它不允许我将数据写入数据库。你知道吗?你知道吗


Tags: andtheselffortimechannelprogram计时器
2条回答

你考虑过sleep模块中的time吗?你知道吗

import time

time.sleep(2) # probably don't need this one
#set the timer for 2 seconds to update the value in the setlabel
self.getControl(4202).setLabel("1%")


time.sleep(2)
#Stop the timer and set the timer again for 2 seconds
self.getControl(4202).setLabel("8%")


time.sleep(2)
#Stop the timer and set the timer again for another 2 seconds
self.getControl(4202).setLabel("16%")

有几个Python调度库可供选择。芹菜是一个非常健壮的同步任务队列和消息系统,支持计划任务。你知道吗

你试过高级Python调度程序(APScheduler)吗 我喜欢。你可以调用函数

@sched.interval_schedule(minutes=3)
def timed_job():
    print 'This job is run every three minutes.'

相关问题 更多 >