如何用Python 的turtle 和 time製作乙個時鐘呢?

時間 2021-12-20 16:43:48

1樓:夕顏碎影

直接上案例:

# coding=utf-8

import turtle

from datetime import *

# 抬起畫筆,向前運動一段距離放下

def Skip(step):

turtle.penup()

turtle.forward(step)

turtle.pendown()

def mkHand(name, length):

# 註冊Turtle形狀,建立表針Turtle

turtle.reset()

Skip(-length * 0.1)

# 開始記錄多邊形的頂點。當前的烏龜位置是多邊形的第乙個頂點。

turtle.begin_poly()

turtle.forward(length * 1.1)

# 停止記錄多邊形的頂點。當前的烏龜位置是多邊形的最後乙個頂點。將與第乙個頂點相連。

turtle.end_poly()

# 返回最後記錄的多邊形。

handForm = turtle.get_poly()

turtle.register_shape(name, handForm)

def Init():

global secHand, minHand, hurHand, printer

# 重置Turtle指向北

turtle.mode("logo")

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

mkHand("secHand", 135)

mkHand("minHand", 125)

mkHand("hurHand", 90)

secHand = turtle.Turtle()

secHand.shape("secHand")

minHand = turtle.Turtle()

minHand.shape("minHand")

hurHand = turtle.Turtle()

hurHand.shape("hurHand")

for hand in secHand, minHand, hurHandhand.shapesize(1, 1, 3hand.speed(0)

# 建立輸出文字Turtle

printer = turtle.Turtle()

# 隱藏畫筆的turtle形狀

printer.hideturtle()

printer.penup()

def SetupClock(radius):

# 建立表的外框

turtle.reset()

turtle.pensize(7)

for i in range(60Skip(radiusif i % 5 == 0turtle.forward(20Skip(-radius - 20Skip(radius + 20if i == 0turtle.write(int(12), align="center", font=("Courier", 14, "bold"elif i == 30Skip(25turtle.

write(int(i / 5), align="center", font=("Courier", 14, "bold"Skip(-25elif (i == 25 or i == 35Skip(20turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"Skip(-20elseturtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"Skip(-radius - 20elseturtle.

dot(5Skip(-radiusturtle.right(6)

def Week(t):

week = ["星期一", "星期二", "星期三൪星期四", "星期五", "星期六", "星期日"]

return week[t.weekday()]

def Date(t):

y = t.year

m = t.month

d = t.day

return "%s.%d.%d" % (y, m, d)

def Tick繪製表針的動態顯示

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)

turtle.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()

turtle.tracer(True)

# 100ms後繼續呼叫tick

turtle.ontimer(Tick, 100)

def main開啟/關閉龜動畫,並為更新圖紙設定延遲。

turtle.tracer(False)

Init()

SetupClock(160)

turtle.tracer(True)

Tick()

turtle.mainloop()

if __name__ == "__main__":

main()

關於turtle

2樓:猿木求漁

用python turtle自帶的例子不好麼

#!/usr/bin/env python3

# -*- coding: cp1252 -*-

""" turtle-example-suitetdemo_clock.py

Enhanced clock-program, showing date

and timePress STOP to exit the program34;""

from turtle import *

from datetime import datetime

def jump(distanz, winkel=0):

penup()

right(winkel)

forward(distanz)

left(winkel)

pendown()

def hand(laenge, spitze):

fd(laenge*1.15)

rt(90)

fd(spitze/2.0)

lt(120)

fd(spitze)

lt(120)

fd(spitze)

lt(120)

fd(spitze/2.0)

def make_hand_shape(name, laenge, spitze):

reset()

jump(-laenge*0.15)

begin_poly()

hand(laenge, spitze)

end_poly()

hand_form = get_poly()

register_shape(name, hand_form)

def clockface(radius):

reset()

pensize(7)

for i in range(60jump(radiusif i % 5 == 0fd(25jump(-radius-25elsedot(3jump(-radiusrt(6)

def setup():

global second_hand, minute_hand, hour_hand, writer

mode("logo")

make_hand_shape("second_hand", 125, 25)

make_hand_shape("minute_hand", 130, 25)

make_hand_shape("hour_hand", 90, 25)

clockface(160)

second_hand = Turtle()

second_hand.shape("second_hand")

second_hand.color("gray20", "gray80")

minute_hand = Turtle()

minute_hand.shape("minute_hand")

minute_hand.color("blue1", "red1")

hour_hand = Turtle()

hour_hand.shape("hour_hand")

hour_hand.color("blue3", "red3")

for hand in second_hand, minute_hand, hour_handhand.resizemode("user"hand.shapesize(1, 1, 3hand.

speed(0)

ht()

writer = Turtle()

#writer.mode("logo")

writer.ht()

writer.pu()

writer.bk(85)

def wochentag(t):

wochentag = ["Monday", "Tuesday", "Wednesday൪Thursday", "Friday", "Saturday", "Sunday"]

return wochentag[t.weekday()]

def datum(z):

monat = ["Jan.", "Feb.", "Mar.

", "Apr.", "May", "June൪July", "Aug.", "Sep.

", "Oct.", "Nov.", "Dec.

"]j = z.year

m = monat[z.month - 1]

t = z.day

return "%s %d %d" % (m, t, j)

def tick():

t = datetime.today()

sekunde = t.second + t.microsecond*0.000001

minute = t.minute + sekunde/60.0

stunde = t.hour + minute/60.0

trytracer(False) # Terminator can occur here

writer.clearwriter.homewriter.

forward(65writer.write(wochentag(talign="center", font=("Courier", 14, "bold"writer.back(150writer.

write(datum(talign="center", font=("Courier", 14, "bold"writer.forward(85tracer(Truesecond_hand.setheading(6*sekunde) # or here

minute_hand.setheading(6*minutehour_hand.setheading(30*stundetracer(Trueontimer(tick, 100)

except Terminatorpass # turtledemo user pressed STOP

def main():

tracer(False)

setup()

tracer(True)

tick()

return "EVENTLOOP"

if __name__ == "__main__":

mode("logo")

msg = main()

print(msg)

mainloop()

如何用python控制arduino?

FLAGYAN Arduino板子的韌體 下位機 在自帶的IDE裡面有,路徑Examples Firmata StandardFirmata,寫入這個韌體就可以在PC上寫python的程式直接控制,python直接執行在PC上,通過frimata協議給板子發命令執行相應的操作。 許俊彬 Arduin...

請問python如何用docker API和paramiko做webssh?

37丫37 或許可以看看這個 Django實現WebSSH操作Kubernetes Podpython 呼叫kubernetes api,實現對pod的ssh連線,從而進行命令執行等操作 還有一系列的WebSSH相關的文章可以參考 https blog.ops coffee.cn webssh 布道...

如何用python傳送email?

若月寒 fp open filename,rb 務必加上必要的頭,否則傳送後無法正常解碼file.add header Content Disposition attachment filename gbk filename file.add header Content ID 0 file.add...