温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python实现简单语音整点报时的方法

发布时间:2021-06-04 10:50:30 阅读:583 作者:小新 栏目:开发技术
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章给大家分享的是有关Python实现简单语音整点报时的方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

主要的技术特殊点在于PyS60的定时器最多只能定2147秒。在手机上直接写的。

import e32
import audio
import time
import appuifw
import sys
import os.path
import marshal
 
def say(oclock):
  """say the time in English"""
  c = oclock
  if c > 12:
    c -= 12
  cs = ['zero''one''two''three''four''five''six''seven''eight''nine''ten''eleven''twelve'][c]
  audio.say("it's " + cs + " o'clock.")
  
def say_current():
  global Sayflags
  t = time.localtime()
  # say according to configuration
  if Sayflags[int(t[3])] == 1:
    say(t[3])
  
def on_oclock():
  """when an o'clock arrived"""
  say_current()
  start_timer()
  
def start_timer():
  """start a timer that will be reached at next o'clock"""
  global Timer
  lt = time.localtime()
  d = 60 * (59 - lt[4]) + 61 - lt[5]
  if d>2147:
    Timer.after(2147lambda : Timer.after(d-2147, on_oclock)) 
  else:
    Timer.after(d, on_oclock)
  
def clock_names():
  return [u'0:00'u'1:00'u'2:00'u'3:00'u'4:00'u'5:00'u'6:00'u'7:00'u'8:00'u'9:00'u'10:00'u'11:00'u'12:00'u'13:00'u'14:00'u'15:00'u'16:00'u'17:00'u'18:00'u'19:00'u'20:00'u'21:00'u'22:00'u'23:00']
  
def list_handler():
  """set flag and refresh the listbox"""
  global Lb
  global Sayflags
  c = Lb.current()
  Sayflags[c] = 1 - Sayflags[c]
  Lb.set_list(list_content(), c)
 
def list_content():
  global Sayflags
  icons = [appuifw.Icon(u"z:\\resource\\apps\\avkon2.mif"1650616507), appuifw.Icon(u"z:\\resource\\apps\\avkon2.mif"1650416505)] # unchecked, unchecked
  return map(lambda s, f: tuple([s, icons[f]]), clock_names(), Sayflags)
  
def exit_handler():
  global Lock
  global Timer
  global Standalone
  Timer.cancel()
  save_cfg()
  if not Standalone:
    Lock.signal()
  else:
    appuifw.app.set_exit()
 
def save_cfg():
  global Sayflags
  try:
    f = open(Configfile, 'wb')
    marshal.dump(Sayflags, f)
    f.close()
  except:
    pass
  
def load_cfg():
  global Sayflags
  try:
    f = open(Configfile, 'rb')
    Sayflags = marshal.load(f)
    f.close()
  except:
    pass
 
# Testing code
def test():
  say_current()
  #on_oclock()
  #for n in range(1,13):
  #  say(n)
#test()
 
def main():
  global Standalone
  appuifw.app.title = u'Audio Clock'
  appuifw.app.exit_key_handler = exit_handler
  appuifw.app.body = Lb
  if time.localtime()[4] == 0:
    say_current()
  start_timer()
  if not Standalone:
    Lock.wait()
  
Standalone = True
Timer = e32.Ao_timer()
Lock = e32.Ao_lock()
Configfile = os.path.abspath(os.path.dirname(sys.argv[0])) + '\\audioclock.cfg'
Sayflags = [000000010001110000100011#24 clocks' flags
load_cfg()
Lb = appuifw.Listbox(list_content(), list_handler)
main()

感谢各位的阅读!关于“Python实现简单语音整点报时的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×