免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 13589 | 回复: 8
打印 上一主题 下一主题

想用python写个简单时钟 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-31 10:43 |只看该作者 |倒序浏览
python输出当前时间比较简单,
我想每隔1秒刷新下时钟,不知道python下有什么简单的方法。

论坛徽章:
0
2 [报告]
发表于 2008-05-31 12:09 |只看该作者
import time,sys
while True:
     s=time.ctime()
     length=len(s)
     sys.stdout.write(s)
     time.sleep(1)
     sys.stdout.write('\b'*length)

论坛徽章:
0
3 [报告]
发表于 2008-05-31 14:46 |只看该作者
原帖由 ajinn 于 2008-5-31 10:43 发表
python输出当前时间比较简单,
我想每隔1秒刷新下时钟,不知道python下有什么简单的方法。


Programming Python, 3rd Edition
  By Mark Lutz


  1. ##########################################################################
  2. # set and catch alarm timeout signals in Python; time.sleep doesn't play
  3. # well with alarm (or signal in general in my Linux PC), so we call
  4. # signal.pause here to do nothing until a signal is received;
  5. ##########################################################################

  6. import sys, signal, time
  7. def now(): return time.ctime(time.time( ))

  8. def onSignal(signum, stackframe):                 # python signal handler
  9.     print 'Got alarm', signum, 'at', now( )       # most handlers stay in effect

  10. while 1:
  11.     print 'Setting at', now( )
  12.     signal.signal(signal.SIGALRM, onSignal)       # install signal handler
  13.     signal.alarm(5)                               # do signal in 5 seconds
  14.     signal.pause( )                               # wait for signals
复制代码

[ 本帖最后由 baif 于 2008-5-31 14:50 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-05-31 16:16 |只看该作者
强人啊!!!!受教了

论坛徽章:
0
5 [报告]
发表于 2008-06-03 11:10 |只看该作者
呼呼~~很不错~~顶起~~~~~

论坛徽章:
0
6 [报告]
发表于 2008-06-09 16:41 |只看该作者


MainPage of signal(2)
SIGNAL(2)                  Linux Programmer’s Manual                 SIGNAL(2)
...
...
DESCRIPTION
       The  behavior of signal() varies across Unix versions, and has also varied historically across different versions of Linux.  Avoid its use: use sigaction(2) instead.  See Portability
       below.
...




Release Notes of Python 2.0
A wrapper API was added for signal() and sigaction(). Instead of either function, always use PyOS_getsig() to get a signal handler and PyOS_setsig() to set one. A new convenience typedef PyOS_sighandler_t is defined for the type of signal handlers.


Documentation of Python API 5.1 Operating System...
PyOS_sighandler_t   PyOS_setsig  ( int i, PyOS_sighandler_t h)
Set the signal handler for signal i to be h; return the old signal handler. This is a thin wrapper around either sigaction() or signal(). Do not call those functions directly! PyOS_sighandler_t is a typedef alias for void (*)(int).

原帖由 baif 于 2008-5-31 14:46 发表


Programming Python, 3rd Edition
  By Mark Lutz


##########################################################################
# set and catch alarm timeout signals in Python; time.sleep do ...

论坛徽章:
0
7 [报告]
发表于 2009-05-21 11:23 |只看该作者
3楼:

你贴的代码运行起来有问题,是这一句:
signal.signal(signal.SIGALRM, onSignal)

提示是:
AttributeError:'Module' object has no attribute 'SIGALRM'

我是在Python 2.6的command line中运行的。
用dir(signal)也看不到上面的属性,不知是什么原因?

论坛徽章:
0
8 [报告]
发表于 2009-05-22 08:12 |只看该作者
原帖由 refeihc 于 2009-5-21 11:23 发表
3楼:

你贴的代码运行起来有问题,是这一句:
signal.signal(signal.SIGALRM, onSignal)

提示是:
AttributeError:'Module' object has no attribute 'SIGALRM'

我是在Python 2.6的command line中运 ...

windows把?

论坛徽章:
0
9 [报告]
发表于 2009-05-25 21:20 |只看该作者
如果是GUI编程,自己弄个钟表界面,然后自己定义时钟就ok了。
  timer = wx.PyTimer(self.Notify())
   timer.Start(1000) #1second
   self.Notify()
   
   def Notify()
   //code here
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP