python做的時鐘

2021-10-05 16:34:16 字數 3260 閱讀 6434

python能幹任何事連鐘錶也可以

from turtle import

*from datetime import

*def

skip

(step)

: penup(

) forward(step)

pendown(

)def

mkhand

(name, length)

:#註冊turtle形狀,建立表針turtle

reset(

) skip(

-length*

0.1)

begin_poly(

) forward(length*

1.1)

end_poly(

) handform = get_poly(

)#註冊turtle形狀命令register_shape(name,shape=none)

register_shape(name, handform)

definit()

:global sechand, minhand, hurhand, printer

mode(

"logo"

)# 重置turtle指向北

#建立三個表針turtle並初始化

#第二個引數為長度

mkhand(

"sechand"

,125

) mkhand(

"minhand"

,130

) mkhand(

"hurhand",90

) sechand = turtle(

) sechand.shape(

"sechand"

) minhand = turtle(

) minhand.shape(

"minhand"

) hurhand = turtle(

) hurhand.shape(

"hurhand"

)for hand in sechand, minhand, hurhand:

hand.shapesize(1,

1,3)

hand.speed(0)

#建立輸出文字turtle

printer = turtle(

) printer.hideturtle(

) printer.penup(

)def

setupclock

(radius)

:#建立表的外框

reset(

) pensize(7)

for i in

range(60

):skip(radius)

if i %5==

0:forward(20)

skip(

-radius-20)

else

: dot(5)

skip(

-radius)

right(6)

defweek

(t):

week =

["星期一"

,"星期二"

,"星期三"

,"星期四"

,"星期五"

,"星期六"

,"星期日"

]return week[t.weekday()]

defdate

(t):

y = t.year

m = t.month

d = t.day

return

"%s %d %d"

%(y, m, d)

deftick()

:#繪製表針的動態顯示

#當前時間

t = datetime.today(

) second = t.second + t.microsecond*

0.000001

minute = t.minute + second/

60.0

hour = t.hour + minute/

60.0

sechand.setheading(

6*second)

minhand.setheading(

6*minute)

hurhand.setheading(

30*hour)

#介入tracer函式以控制重新整理速度

tracer(

false

) printer.forward(65)

printer.write(week(t)

, align=

"center"

, font=

("courier",14

,"bold"))

printer.back(

130)

printer.write(date(t)

, align=

"center"

, font=

("courier",14

,"bold"))

printer.home(

) tracer(

true

) ontimer(tick,

100)

#100ms後繼續呼叫tick

defmain()

: tracer(

false

) init(

) setupclock(

160)

tracer(

true

) tick(

) mainloop(

)if __name__ ==

"__main__"

:

main(

)import time,sys,os

while(1

):t = time.strftime(

'%h:%m:%s'

,time.localtime(time.time())

) sys.stdout.write(t+

'\b'*10

) sys.stdout.flush(

) time.sleep(

0.1)

os.system(

'cls'

)

效果圖:

Python 時鐘程式

直接上 import time h int input 請輸入小時 m int input 請輸入分鐘 s int input 請輸入秒數 class clock object 數字時鐘 def init self,hour 0,minite 0,second 0 時鐘 self.hour hour...

python時鐘原始碼

話不多說,如下 encoding utf 8 version author file 時鐘.py time 2020 4 9 21 43 import sys,random,math,pygame from pygame.locals import from datetime import date...

python程式設計之 模擬時鐘

運用畫圓,畫線的方法,以及一些數學知識做的乙個demo。對教材裡的例子裡的畫時針的部分修改了下。需要注意的是遠點選擇在 而數字時鐘的十二點位於遠點的正上方。所以如果用三角函式來計算時針分針秒針的位置的時候需要對角度進行旋轉。根據sin,cos函式的特點,將對應的角度 90 讀得到的是所需要的結果。比...