1. 2005-09-28 "土制闹钟"

发件人: Kai <[email protected]> 
回复: [email protected]
收件人: "[email protected]" <[email protected]>
日期: 2005-9-27 下午11:48
主题: [python-chinese] 我的土制闹钟

为了避免连续长时间看电脑,能让可怜的眼睛休息一下,我凑合出这个python程序(完全新手啊,汗,大伙给改改。而且还缺少unix上的播放命令)。 一开机,就让它一直运行,每过60分钟,它就随机选一首曲子播放,提醒歇会,看个三级写真照片什么的。

   1 
   2 
   3 #!/usr/bin/python
   4 # -*- coding: gb2312 -*-
   5 
   6 import sys
   7 import time
   8 import random
   9 import os
  10 
  11 directory = 'c:\\downloads\\music'  # 歌曲文件夹
  12 
  13 def play_soundfile(filename):  # 启动播放器
  14    if sys.platform == 'win32':
  15        import win32api
  16        win32api.ShellExecute(0, "open", filename, None, "", 0)
  17    else:
  18        print "some *nix commands here to play a sound file" # 需要加上在unix 上播放的命令
  19 
  20 fileList =  [os.path.join(directory, os.path.normcase(f)) for f in os.listdir(directory)]  # get a list of all music files
  21 
  22 while 1:
  23    print "--------------------------------------"
  24 
  25    fileName = random.choice(fileList)
  26    print fileName
  27 
  28    play_soundfile(fileName)
  29 
  30    time.sleep(1800)
  31    print '30 minutes have passed. ', time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
  32    time.sleep(1800)
  • goopler: in order to import win32api, one needs to "要安装win32all包。这里有:

http://starship.python.net/crew/mhammond/win32/Downloads.html"

  • 张骏:pyw后缀就可以去掉console窗口
  • makeyunbad:

os.startfile(filename)

os.startfile()好像调用的就是win32api.ShellExecute()

startfile( path)

Start a file with its associated application. This acts like double-clicking the file in Windows Explorer, or giving the file name as an argument to the start command from the interactive command shell: the file is opened with whatever application (if any) its extension is associated. startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application's exit status. The path parameter is relative to the current directory. If you want to use an absolute path, make sure the first character is not a slash ("/"); the underlying Win32 ShellExecute() function doesn't work if it is. Use the os.path.normpath() function to ensure that the path is properly encoded for Win32. Availability: Windows. New in version 2.0.

  • limodou: 可以考虑使用wxPython来做,可以设置定时器,可以最小化,可以放声音。
  • 杨军: 我现在用的是Workrave ,感觉也不错,有兴趣的可以试试。

http://www.workrave.org/welcome/

  • panhudie:

这个是用twisted的task来作, 随便把ipython放到里面, 这样还可以当ipython用.

   1 # -*- coding: utf-8 -*-
   2 
   3 from IPython.Shell import IPShellEmbed
   4 args = ['-pi1','In <\\#>:','-pi2','    .\\D.:','-po','Out<\\#>:','-nosep']
   5 ipshell = IPShellEmbed(args)
   6 ipshell.set_exit_msg('Ctrl-C to exit program')
   7 ipshell.set_banner('Ctrl-D to exit interpreter and continue program')
   8 from twisted.internet import reactor, threads, task
   9 
  10 shell = threads.deferToThread(ipshell)  #打开ipython
  11 
  12 player = task.LoopingCall(play_soundfile)  #这里是你要打开的程序
  13 
  14 player.start(60*30)              #每1800秒播放一次
  15 
  16 #可以用 player.stop() 来中止
  17 reactor.run()