需改 turtle繪製表盤

2021-08-17 23:01:37 字數 3205 閱讀 6007

debug了好久,最後終於表盤時鐘走起。

**參考如下

import turtle

from datetime import *

#up down 表示畫筆抬起放下的過程,中間走了一定的步數,這裡表示行走的過程

def skip(step):

turtle.penup()

turtle.forward(step)

turtle.pendown()

#建立表針,

def mkhand (name,length):

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.mode("logo")

# 三種模式:standard,logo,world。 standard: 向右(朝東) 逆時針

# logo : 向上(朝北) 順時針

mkhand("sechand",135) #sechand表示名稱秒針,135表示長度

mkhand("minhand",125) #分針,125長度

mkhand("hurhand",90) #時針,90長度

sechand=turtle.turtle()

sechand.shape("sechand")

minhand=turtle.turtle()

minhand.shape("minhand")

hurhand=turtle.turtle()

hurhand.shape("hurhand")

for hand in sechand, minhand, hurhand:

hand.shapesize(1, 1, 3)

hand.speed(0)

printer=turtle.turtle()

printer.hideturtle()

printer.penup()

#這裡是繪製表盤內邊一圈的點點

def setupclock(radius):

turtle.reset() #清空之前的內容

turtle.pensize(7) #表示畫筆的粗細

for i in range(60): #60個點依次迴圈

skip(radius)

if i%5==0: #如果是5的倍數,即為整點部分

turtle.forward(20)

skip(-radius -20)

skip(radius +20)

if i==0:

turtle.write(int(12),align="center",font=("courier",14,"bold"))

elif i==30:

skip(25)

turtle.write(int(i/5),align="center",font=("courier",14,"bold"))

skip(-25)

elif(i==25 or i==35):

skip(20)

turtle.write(int(i/5),align="center",font=("courier",14,"bold"))

skip(-20)

else:

turtle.write(int (i/5),align="center",font=("courier",14,"bold"))

skip(-radius -20)

else:

turtle.dot(5)

skip(-radius)

turtle.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 繪製愛心

import turtle import time 畫愛心的頂部 deflittleheart for i in range 200 turtle.right 1 turtle.forward 2 輸入表白的語句,預設i love you love input please enter a sent...

Turtle繪製分形樹

import turtle def draw branch branch length if branch length 5 限定繪製的樹枝 包括樹幹 樹枝和樹葉 長度至少大於5 if branch length 20 如果長度小於20,即可判定是樹葉,繪製成綠色 turtle.color gree...

Python 使用turtle繪製多重巢狀的六邊形

import turtle import math import numpy as np 設定turtle,及定義顏色表 turtle.pensize 1 設定線的粗細 turtle.shape turtle 設定turtle形狀 colors red yellow green blue black...