溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python如何開發(fā)一個(gè)動態(tài)時(shí)鐘小程序

發(fā)布時(shí)間:2021-11-25 13:56:39 來源:億速云 閱讀:134 作者:小新 欄目:大數(shù)據(jù)

這篇文章給大家分享的是有關(guān)Python如何開發(fā)一個(gè)動態(tài)時(shí)鐘小程序的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

前言

本文的文字及圖片來源于網(wǎng)絡(luò),僅供學(xué)習(xí)、交流使用

import turtle
from datetime import *
# 抬起畫筆,向前運(yùn)動一段距離放下
def skip(step):
    turtle.penup()
    turtle.forward(step)
    turtle.pendown()
def mkHand(name, length):
    # 注冊Turtle形狀,建立表針Turtle
    turtle.reset()
    # 先反向運(yùn)動一段距離,終點(diǎn)作為繪制指針的起點(diǎn)
    skip(-length * 0.1)
    # 開始記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的第一個(gè)頂點(diǎn)。
    turtle.begin_poly()
    turtle.forward(length * 1.1)
    # 停止記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的最后一個(gè)頂點(diǎn)。將與第一個(gè)頂點(diǎn)相連。
    turtle.end_poly()
    # 返回最后記錄的多邊形。
    handForm = turtle.get_poly()
    turtle.register_shape(name, handForm)


def init():
    global secHand, minHand, houHand, printer
    # 重置Turtle指向北
    turtle.mode("logo")
    # 建立三個(gè)表針Turtle并初始化
    mkHand("secHand", 135)
    mkHand("minHand", 125)
    mkHand("houHand", 90)
    secHand = turtle.Turtle()
    secHand.shape("secHand")
    minHand = turtle.Turtle()
    minHand.shape("minHand")
    houHand = turtle.Turtle()
    houHand.shape("houHand")

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

    # 建立輸出文字Turtle
    printer = turtle.Turtle()
    # 隱藏畫筆的turtle形狀
    printer.hideturtle()
    printer.penup()


# 繪制表盤
def setupClock(radius):
    # 建立表的外框
    turtle.reset()
    turtle.pensize(7)
    for i in range(60):
        # 向前移動半徑值
        skip(radius)
        if i % 5 == 0:
            # 畫長刻度線
            turtle.forward(20)
            # 回到中心點(diǎn)
            skip(-radius - 20)

            # 移動到刻度線終點(diǎn)
            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"))

            # 回到中心點(diǎn)
            skip(-radius - 20)
        else:
            # 畫圓點(diǎn)
            turtle.dot(5)
            skip(-radius)
        # 順時(shí)針移動6°
        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ài)顯示
    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)
    houHand.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后繼續(xù)調(diào)用tick
    turtle.ontimer(tick, 100)


def main():
    # 打開/關(guān)閉龜動畫,并為更新圖紙?jiān)O(shè)置延遲。
    turtle.tracer(False)
    init()
    setupClock(160)
    turtle.tracer(True)
    tick()
    turtle.mainloop()


if __name__ == "__main__":
    main()

感謝各位的閱讀!關(guān)于“Python如何開發(fā)一個(gè)動態(tài)時(shí)鐘小程序”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI