想用python写个简单时钟

想用python写个简单时钟

python输出当前时间比较简单,
我想每隔1秒刷新下时钟,不知道python下有什么简单的方法。
import time,sys
while True:
     s=time.ctime()
     length=len(s)
     sys.stdout.write(s)
     time.sleep(1)
     sys.stdout.write('\b'*length)


QUOTE:
原帖由 ajinn 于 2008-5-31 10:43 发表
python输出当前时间比较简单,
我想每隔1秒刷新下时钟,不知道python下有什么简单的方法。

Programming Python, 3rd Edition
  By Mark Lutz

[Copy to clipboard] [ - ]
CODE:
##########################################################################
# set and catch alarm timeout signals in Python; time.sleep doesn't play
# well with alarm (or signal in general in my Linux PC), so we call
# signal.pause here to do nothing until a signal is received;
##########################################################################

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

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

while 1:
    print 'Setting at', now( )
    signal.signal(signal.SIGALRM, onSignal)       # install signal handler
    signal.alarm(5)                               # do signal in 5 seconds
    signal.pause( )                               # wait for signals

强人啊!!!!受教了
呼呼~~很不错~~顶起~~~~~


MainPage of signal(2)

QUOTE:
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

QUOTE:
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...

QUOTE:
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).



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


Programming Python, 3rd Edition
  By Mark Lutz


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