欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Python制作三款起床鬧鐘的示例代碼

 更新時(shí)間:2022年05月14日 09:24:18   作者:顧木子吖  
每天上班最痛苦的事情就是早起早起早起!這是大部分上班族的痛苦,但是不上班又是不可能的啦,因?yàn)槎际菫榱烁沐X(qián)。本文用Python制作了三款有趣的鬧鐘,感興趣的可以學(xué)習(xí)一下

導(dǎo)語(yǔ)

叮叮叮,我們要按時(shí)長(zhǎng)大

我是你們的木子同學(xué)!當(dāng)當(dāng)當(dāng)當(dāng)——隆重出場(chǎng),撒花撒花~

嗨!大家有沒(méi)有生物鐘不準(zhǔn)時(shí)的時(shí)候,是不是每到休息日或者長(zhǎng)假就會(huì)經(jīng)常要倒時(shí)差?

每天上班最痛苦的事情就是早起早起早起!這是大部分上班族的痛苦,但是不上班又是不可能的啦,因?yàn)槎际菫榱烁沐X(qián)

今天小編就用代碼示例化,給大家展示一下不同的時(shí)鐘,希望大家按時(shí)上班,按時(shí)搞錢(qián)啦

一、Turtle繪制時(shí)鐘

1)代碼展示

import turtle
from datetime import *
 
# 抬起畫(huà)筆,向前運(yùn)動(dòng)一段距離放下
def Skip(step):
    turtle.penup()
    turtle.forward(step)
    turtle.pendown()
 
def mkHand(name, length):
    # 注冊(cè)Turtle形狀,建立表針Turtle
    turtle.reset()
    Skip(-length * 0.1)
    # 開(kāi)始記錄多邊形的頂點(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, hurHand, printer
    # 重置Turtle指向北
    turtle.mode("logo")
    # 建立三個(gè)表針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, hurHand:
        hand.shapesize(1, 1, 3)
        hand.speed(0)
 
    # 建立輸出文字Turtle
    printer = turtle.Turtle()
    # 隱藏畫(huà)筆的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)
            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.da
    return "%s %d%d" % (y, m, d)
 
def Tick():
    # 繪制表針的動(dòng)態(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)
    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.hom()
    turtle.tracer(True)
 
    # 100ms后繼續(xù)調(diào)用tick
    turtle.ontimer(Tick, 100)
 
def main():
    # 打開(kāi)/關(guān)閉龜動(dòng)畫(huà),并為更新圖紙?jiān)O(shè)置延遲。
    turtle.tracer(False)
    Init()
    SetupClock(160)
    turtle.tracer(True)
    Tick()
    turtle.mainloop()
 
if __name__ == "__main__":
    main()

2)效果展示

二、Turtle實(shí)現(xiàn)模擬時(shí)鐘

1)代碼展示

基本思路:自定義shape,畫(huà)出時(shí)針、分針、秒針。通過(guò)register_shape()函數(shù)。 指針的位置通過(guò)shape類setheading()函數(shù)進(jìn)行設(shè)置。 獲取系統(tǒng)時(shí)間作為當(dāng)前時(shí)間,datetime.today()。 利用ontimer()函數(shù)定義定時(shí)器事件,觸發(fā)屏幕更新。 利用write()函數(shù)在屏幕上顯示文本。

from turtle import *
from datetime import datetime
 
mode("logo") # 向上(北),正角度為順時(shí)針
 
thisday = 0
thisecond = 0
 
second_hand = Turtle()
minute_hand = Turtle()
hour_hand = Turtle()
writer = Turtle()
writer.getscreen().bgcolor('gray90')
writer.color("gray20", "gray20")
 
def jump(distanz, winkel=0):
    penup()
    right(winkel)
    forward(distanz)
    left(winkel)
    pendown()
'''
laenge 指針長(zhǎng)度
width 指針寬度
spitze 箭頭邊長(zhǎng)
'''
def hand(laenge, spitze, width):
    lt(90)
    fd(width)
    rt(90)
    fd(laenge*1.15)
    rt(90)
    fd(width * 2)
    rt(90)
    fd(laenge*1.15)
    rt(90)
    fd(width)
    rt(90)
    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, width):
    reset()
    jump(-laenge*0.15) # 指針靠近表盤(pán)中心的末端,但不與圓心重合
    begin_poly()
    hand(laenge, spitze, width)
    end_poly()
    hand_form = get_poly()
    register_shape(name, hand_form)
 
 
def clockface(radius):
    reset()
    # 外圓周
    pensize(2)
    colors = ['green3', 'green2', 'gray98']
    # 從外向內(nèi)fill
    for i in range(3):
        jump(radius+7+(2-i)*4,90)
        fillcolor(colors[i])
        begin_fill()
        circle(radius+7+(2-i)*4, steps=1000)
        end_fill()
        jump(-radius-7-(2-i)*4,90)
 
    # 刻度
    pensize(7)
    color("gray60", "gray60")
    # 經(jīng)驗(yàn)值
    params = [-35, -40, -40, -25, -15, -5, 0, -5, -15, -25, -40, -40] #距離
    angles = [0, -15, -25, -40, -35, -30, 0, 30, 35, 40, 25, 15] # 角度
    for i in range(60):
        jump(radius)
        if i % 5 == 0:
            fd(-15)
            # 下面三行寫(xiě)表盤(pán)數(shù)字
            jump(params[i/5], angles[i/5])
            write(12 if i/5==0 else i/5, align="center", font=("Courier", 20, "bold"))
            jump(params[i/5], 180+angles[i/5])
            jump(-radius+15)
        else:
            dot(3)
            jump(-radius)
        rt(6)
 
 
def setup():
    global second_hand, minute_hand, hour_hand, writer
    # 自定義形狀
    make_hand_shape("hour_hand", 90, 25, 5)
    make_hand_shape("minute_hand",  130, 25, 3)
    make_hand_shape("second_hand", 140, 10, 1)
 
    # 畫(huà)表盤(pán)
    clockface(160)
 
    hour_hand.shape("hour_hand")
    hour_hand.color("gray30", "gray12")
 
    minute_hand.shape("minute_hand")
    minute_hand.color("gray40", "blue")
 
    second_hand.shape("second_hand")
    second_hand.color("red4", "red4")
 
    for hand in hour_hand, minute_hand, second_hand:
        hand.resizemode("user")
        hand.shapesize(1, 1, 1)
        hand.speed(1)
    ht()
 
    writer.ht()
    writer.pu()
    writer.bk(85)
    
def wochentag():
    wochentag = ["星期一", "星期二", "星期三","星期四", "星期五", "星期六", "星期日"]
    return wochentag[t.weekday()]
 
def get_mmdd(z):
    m = z.month
    t = z.day
    return "%d月%d日" % (m, t)
 
def get_yyyy(z):
    j = z.year
    return "%d" % (j)
 
def write_date(t):
    global thisday
    x = t.day
    if thisday != x:
        thisday = x
        writer.clear()
        writer.home()
        writer.forward(65)
        writer.write(wochentag(t),
                 align="center", font=("Courier", 16, "bold"))
        writer.back(150)
        writer.write(get_mmdd(t),
                 align="center", font=("Courier", 16, "normal"))
        writer.back(15)
        writer.write(get_yyyy(t),
                 align="center", font=("Courier", 10, "normal"))
        writer.forward(100)
 
def tick():
    global thisecond
    t = datetime.today()
    if thisecond != t.second:
        thisecond = t.second
        #print t
        sekunde = t.second + t.microsecond * 0.000001
        minute = t.minute + sekunde / 60.0
        stunde = t.hour + minute / 60.0
        tracer(False)
        write_date(t)
        tracer(True)
        hour_hand.setheading(30 * stunde)
        minute_hand.setheading(6 * minute)
        second_hand.setheading(6 * sekunde)
    ontimer(tick, 10)
 
def main():
    tracer(False)
    setup()
    tracer(True)
    tick()
    return "EVENTLOOP"
 
if __name__ == "__main__":
    msg = main()
    print msg
    mainloop()

2)效果展示

三、簡(jiǎn)易時(shí)鐘

1)代碼展示

# coding=utf-8
import turtle
from datetime import *
 
 
# 由于表盤(pán)刻度不連續(xù),需頻繁抬起畫(huà)筆,放下畫(huà)筆
def skip(step):
    turtle.penup()  # 畫(huà)筆抬起
    turtle.forward(step)  # 畫(huà)筆移動(dòng)step
    turtle.pendown()  # 畫(huà)筆落下
 
 
# 建立表針,定制表針形狀和名字
def make_hand(name, length):
    turtle.reset()
 
    skip(-length * 0.1)  # 表針一端,過(guò)表盤(pán)中心一小段,開(kāi)始繪制
    turtle.begin_poly()  # 開(kāi)始記錄多邊形的第一個(gè)頂點(diǎn)。
    turtle.forward(length * 1.1)  # 設(shè)置表針長(zhǎng)度,繪制表針
    turtle.end_poly()  # 停止記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的最后一個(gè)頂點(diǎn)。將與第一個(gè)頂點(diǎn)相連。
 
    handForm = turtle.get_poly()  # 返回最后記錄的形狀
 
    turtle.color('black')
    turtle.register_shape(name, handForm)
 
 
# 三個(gè)表針初始化,實(shí)例化
def init_hand():
    global sec_hand, min_hand, hou_hand, printer
    # 重置Turtle指向北
    turtle.mode("logo")  # logo:向上(北) 順時(shí)針   standard:向右(東)  逆時(shí)針
 
    # 建立三個(gè)表針Turtle并初始化
    make_hand("sec_Hand", 135)
    make_hand("min_Hand", 110)
    make_hand("hou_Hand", 70)
 
    sec_hand = turtle.Turtle()
    sec_hand.shape("sec_Hand")
    min_hand = turtle.Turtle()
    min_hand.shape("min_Hand")
    hou_hand = turtle.Turtle()
    hou_hand.shape("hou_Hand")
 
    # 筆的屬性
    for hand in sec_hand, min_hand, hou_hand:
        hand.shapesize(1, 1, 3)
        hand.speed(0)
 
    # 建立輸出打印的文字Turtle
    printer = turtle.Turtle()
 
    # 隱藏畫(huà)筆的turtle形狀
    printer.hideturtle()
    printer.penup()
 
 
# 設(shè)置表盤(pán)
def set_clock(radius):
    turtle.reset()
    turtle.pencolor('red')  # 設(shè)置畫(huà)筆顏色
    turtle.fillcolor('pink')  # 設(shè)置繪制圖形的填充顏色
    turtle.pensize(10)  # 畫(huà)筆寬度
 
    for i in range(60):
        skip(radius)
        # 逢五 使用線條并加粗
        if i % 5 == 0:
            turtle.forward(20)
            skip(-radius - 20)
            skip(radius + 20)
 
            # 設(shè)置數(shù)字的位置及字體,大小
            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)
        # 非五,以點(diǎn)代替
        else:
            turtle.dot(5)
            skip(-radius)
        turtle.right(6)
 
 
# 顯示星期
def show_week(t):
    week = ["星期一  Mon", "星期二  Tue", "星期三  Wed", "星期四  Thu", "星期五  Fri", "星期六  Sat", "星期日  Sun"]
    return week[t.weekday()]  # t.weekday() 周一為0,周二為1...可作為列表的index
 
 
# 顯示日期
def show_data(t):
    y = t.year
    m = t.month
    d = t.day
    return "{} 年 {} 月 {} 日".format(y, m, d)
 
 
# 顯示時(shí)間
# def show_time(t):
#     m = t.minute
#     h = t.hour
#     return "{}:{}".format(h, m)
 
 
# 顯示整個(gè)時(shí)鐘
def show_clock():
    # 獲取時(shí)間
    t = datetime.today()
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second / 60.0
    hour = t.hour + minute / 60.0
 
    sec_hand.setheading(6 * second)
    min_hand.setheading(6 * minute)
    hou_hand.setheading(30 * hour)
 
    turtle.tracer(False)
 
    printer.forward(65)
    printer.write(show_week(t), align='center', font=("Courier", 14, "bold"))
 
    printer.back(65)
    printer.write("The Clock of Hua", align="center", font=("Courier", 16, "bold"))
 
    printer.back(65)
    printer.write(show_data(t), align='center', font=("Courier", 14, "bold"))
 
    # printer.back(25)
    # printer.write(show_time(t), align="center", font=("Courier", 14, "bold"))
    # 回到原點(diǎn),以便于下一輪的顯示
    printer.home()
    turtle.tracer(True)
 
    # 100ms后繼續(xù)調(diào)用show_clock
    turtle.ontimer(show_clock, 100)
 
 
# main函數(shù)
def main():
    turtle.tracer(False)
    # 設(shè)置背景
    ts = turtle.getscreen()
    ts.bgcolor("#cccccc")
    # 初始化
    init_hand()
    # 設(shè)置時(shí)鐘
    set_clock(180)
    turtle.tracer(True)
    # 顯示時(shí)鐘
    show_clock()
    turtle.mainloop()
 
 
if __name__ == "__main__":
    main()

2)效果展示

以上就是基于Python制作三款起床鬧鐘的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python起床鬧鐘的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python虛擬環(huán)境venv用法詳解

    Python虛擬環(huán)境venv用法詳解

    這篇文章主要介紹了Python虛擬環(huán)境venv用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Python帶動(dòng)態(tài)參數(shù)功能的sqlite工具類

    Python帶動(dòng)態(tài)參數(shù)功能的sqlite工具類

    這篇文章主要介紹了Python帶動(dòng)態(tài)參數(shù)功能的sqlite工具類,涉及Python針對(duì)sqlite數(shù)據(jù)庫(kù)的連接、查詢、sql語(yǔ)句執(zhí)行等相關(guān)操作封裝與使用技巧,需要的朋友可以參考下
    2018-05-05
  • python 將html轉(zhuǎn)換為pdf的幾種方法

    python 將html轉(zhuǎn)換為pdf的幾種方法

    這篇文章主要介紹了python 將html轉(zhuǎn)換為pdf的幾種方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • 基于python實(shí)現(xiàn)判斷字符串是否數(shù)字算法

    基于python實(shí)現(xiàn)判斷字符串是否數(shù)字算法

    這篇文章主要介紹了基于python實(shí)現(xiàn)判斷字符串是否數(shù)字算法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 使用go和python遞歸刪除.ds store文件的方法

    使用go和python遞歸刪除.ds store文件的方法

    使用python和go遞歸刪除.DS_Store文件,.DS_Store (英文全稱 Desktop Services Store)是一種由蘋(píng)果公司的Mac OS X操作系統(tǒng)所創(chuàng)造的隱藏文件,目的在于存貯文件夾的自定義屬性
    2014-01-01
  • Python用zip函數(shù)同時(shí)遍歷多個(gè)迭代器示例詳解

    Python用zip函數(shù)同時(shí)遍歷多個(gè)迭代器示例詳解

    這篇文章主要給大家進(jìn)行介紹了Python如何用zip函數(shù)同時(shí)遍歷多個(gè)迭代器,文中給出了示例以及原理和注意事項(xiàng),相信會(huì)對(duì)大家的理解和學(xué)習(xí)很有幫助,有需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11
  • 詳解python中各種文件打開(kāi)模式

    詳解python中各種文件打開(kāi)模式

    在python中,總的來(lái)說(shuō)有三種大的模式打開(kāi)文件,分別是:a, w, r,這篇文章主要介紹了python中各種文件打開(kāi)模式,需要的朋友可以參考下
    2020-01-01
  • python實(shí)現(xiàn)校園網(wǎng)自動(dòng)登錄的示例講解

    python實(shí)現(xiàn)校園網(wǎng)自動(dòng)登錄的示例講解

    下面小編就為大家分享一篇python實(shí)現(xiàn)校園網(wǎng)自動(dòng)登錄的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • 關(guān)于Python參數(shù)解析器argparse的應(yīng)用場(chǎng)景

    關(guān)于Python參數(shù)解析器argparse的應(yīng)用場(chǎng)景

    這篇文章主要介紹了關(guān)于Python參數(shù)解析器argparse的應(yīng)用場(chǎng)景,argparse 模塊使編寫(xiě)用戶友好的命令行界面變得容易,程序定義了所需的參數(shù),而 argparse 將找出如何從 sys.argv 中解析這些參數(shù),需要的朋友可以參考下
    2023-08-08
  • Python中pygame游戲模塊的用法詳解

    Python中pygame游戲模塊的用法詳解

    Pygame是一組用來(lái)開(kāi)發(fā)游戲軟件的 Python 程序模塊,Pygame 在 SDL(Simple DirectMedia Layer) 的基礎(chǔ)上開(kāi)發(fā)而成,它提供了諸多操作模塊,本文給大家介紹了Python中pygame游戲模塊的用法,需要的朋友可以參考下
    2024-01-01

最新評(píng)論