最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python使用Turtle實(shí)現(xiàn)精確計(jì)時(shí)工具

 更新時(shí)間:2025年05月25日 11:25:20   作者:笨笨輕松熊  
這篇文章主要為大家詳細(xì)介紹了Python如何使用Turtle實(shí)現(xiàn)精確計(jì)時(shí)工具,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下

功能特點(diǎn)

數(shù)字顯示當(dāng)前計(jì)時(shí)(時(shí):分:秒.毫秒)

支持開(kāi)始/暫停計(jì)時(shí)(空格鍵)

支持重置計(jì)時(shí)(R鍵)

狀態(tài)顯示(正在計(jì)時(shí)/已暫停/按空格鍵開(kāi)始計(jì)時(shí))

清晰的操作提示

使用方法

運(yùn)行程序后,會(huì)顯示初始時(shí)間(00:00:00.000)

按下空格鍵開(kāi)始計(jì)時(shí)

再次按下空格鍵暫停計(jì)時(shí)

按下R鍵可以重置計(jì)時(shí)器回到00:00:00.000

程序架構(gòu)設(shè)計(jì)

1.核心模塊:

turtle: 提供跨平臺(tái)的圖形化界面繪制功能

time: 提供高精度的系統(tǒng)時(shí)間獲取功能

2.設(shè)計(jì)模式:

采用事件驅(qū)動(dòng)編程模型,通過(guò)鍵盤(pán)事件觸發(fā)狀態(tài)變更

利用全局狀態(tài)管理計(jì)時(shí)器狀態(tài)

3.算法原理:

時(shí)間計(jì)算:利用系統(tǒng)時(shí)間差值計(jì)算經(jīng)過(guò)時(shí)間

狀態(tài)管理:使用布爾變量控制計(jì)時(shí)器運(yùn)行狀態(tài)

代碼詳解

窗口和畫(huà)筆創(chuàng)建

import turtle
import time

# 設(shè)置窗口和基本參數(shù)
screen = turtle.Screen()
screen.title("簡(jiǎn)易計(jì)時(shí)器/秒表")
screen.bgcolor("black")
screen.setup(width=500, height=300)
screen.tracer(0)  # 關(guān)閉自動(dòng)刷新

# 創(chuàng)建顯示時(shí)間的畫(huà)筆
time_display = turtle.Turtle()
time_display.hideturtle() # 隱藏turtle的箭頭圖標(biāo),使繪圖時(shí)只顯示圖形而不顯示箭頭
time_display.color("cyan") # 設(shè)置turtle繪圖的顏色
time_display.penup()   #抬起turtle的筆,這樣移動(dòng)turtle時(shí)不會(huì)繪制線條
time_display.goto(0, 30)  # 將turtle移動(dòng)到坐標(biāo)(0, 30)的位置,準(zhǔn)備從該點(diǎn)開(kāi)始繪圖或顯示文本

# 創(chuàng)建狀態(tài)顯示的畫(huà)筆
status_display = turtle.Turtle()
status_display.hideturtle()
status_display.speed(0)
status_display.color("white")
status_display.penup()
status_display.goto(0, -30)

# 創(chuàng)建操作提示的畫(huà)筆
help_display = turtle.Turtle()
help_display.hideturtle()
help_display.speed(0)
help_display.color("yellow")
help_display.penup()
help_display.goto(0, -80)
help_display.write("空格鍵: 開(kāi)始/暫停 | R鍵: 重置", align="center", font=("Arial", 14, "normal"))

# 初始化秒表變量
is_running = False
start_time = 0
accumulated_time = 0

應(yīng)用知識(shí)點(diǎn):

  • Turtle圖形對(duì)象: 創(chuàng)建多個(gè)Turtle對(duì)象分別負(fù)責(zé)不同UI元素的顯示
  • 窗口控制: 使用screen.tracer(0)關(guān)閉自動(dòng)刷新以提高性能
  • 坐標(biāo)系統(tǒng): 使用二維坐標(biāo)系統(tǒng)合理布局界面元素
  • 全局狀態(tài)變量: 使用全局變量跟蹤計(jì)時(shí)器狀態(tài)

時(shí)間和狀態(tài)顯示更新

# 更新時(shí)間顯示
def update_time_display(elapsed_time):
    hours = int(elapsed_time / 3600)
    minutes = int((elapsed_time % 3600) / 60)
    seconds = int(elapsed_time % 60)
    milliseconds = int((elapsed_time * 1000) % 1000)
    
    time_str = f"{hours:02d}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}"
    time_display.clear()
    time_display.write(time_str, align="center", font=("Arial", 40, "bold"))

# 更新?tīng)顟B(tài)顯示
def update_status():
    status_display.clear()
    if is_running:
        status = "正在計(jì)時(shí)..."
    else:
        if accumulated_time > 0:
            status = "已暫停"
        else:
            status = "按空格鍵開(kāi)始計(jì)時(shí)"
    status_display.write(status, align="center", font=("Arial", 20, "normal"))

應(yīng)用知識(shí)點(diǎn):

  • 時(shí)間單位轉(zhuǎn)換: 將秒數(shù)轉(zhuǎn)換為時(shí)、分、秒、毫秒
  • 取余與整除運(yùn)算: 使用整除和取余操作進(jìn)行時(shí)間單位分解
  • 字符串格式化: 使用f-string和格式說(shuō)明符控制顯示格式
  • 狀態(tài)條件判斷: 根據(jù)多種狀態(tài)組合提供不同的用戶(hù)反饋

計(jì)時(shí)器控制邏輯

def toggle_timer():
    global is_running, start_time, accumulated_time
    
    if is_running:
        # 暫停計(jì)時(shí)
        is_running = False
        accumulated_time += time.time() - start_time
    else:
        # 開(kāi)始計(jì)時(shí)
        is_running = True
        start_time = time.time()
    
    update_status()

應(yīng)用知識(shí)點(diǎn):

  • 全局變量修改: 使用global關(guān)鍵字在函數(shù)內(nèi)修改全局狀態(tài)
  • 高精度時(shí)間獲取: 使用time.time()獲取高精度Unix時(shí)間戳
  • 狀態(tài)切換設(shè)計(jì): 實(shí)現(xiàn)簡(jiǎn)潔的狀態(tài)切換邏輯

計(jì)時(shí)器重置功能

def reset_timer():
    global is_running, start_time, accumulated_time
    is_running = False
    accumulated_time = 0
    start_time = 0
    update_status()
    update_time_display(0)

應(yīng)用知識(shí)點(diǎn):

  • 狀態(tài)重置: 將所有計(jì)時(shí)相關(guān)變量重置為初始值
  • 函數(shù)復(fù)用: 復(fù)用更新顯示的函數(shù),避免代碼冗余

事件監(jiān)聽(tīng)設(shè)置

# 設(shè)置鍵盤(pán)事件
screen.listen()
screen.onkeypress(toggle_timer, "space")  # 空格鍵開(kāi)始/暫停
screen.onkeypress(reset_timer, "r")       # R鍵重置

應(yīng)用知識(shí)點(diǎn):

  • 事件監(jiān)聽(tīng)機(jī)制: 使用listen()方法啟用事件監(jiān)聽(tīng)
  • 回調(diào)函數(shù)綁定: 將鍵盤(pán)事件與特定函數(shù)綁定
  • 函數(shù)引用傳遞: 將函數(shù)名作為參數(shù)傳遞,不帶括號(hào)

主循環(huán)及運(yùn)行控制

def main():
    # 初始顯示
    update_status()
    update_time_display(0)
    
    while True:
        if is_running:
            elapsed_time = accumulated_time + (time.time() - start_time)
        else:
            elapsed_time = accumulated_time
        
        update_time_display(elapsed_time)
        screen.update()
        time.sleep(0.01)  # 小延遲減輕CPU負(fù)擔(dān)

???????# 啟動(dòng)程序
if __name__ == "__main__":
    try:
        main()
    except:
        print("程序已退出")
    turtle.done() 

應(yīng)用知識(shí)點(diǎn):

  • 無(wú)限循環(huán): 使用while True創(chuàng)建游戲主循環(huán)
  • 條件計(jì)時(shí): 根據(jù)運(yùn)行狀態(tài)計(jì)算不同的時(shí)間值
  • 手動(dòng)屏幕刷新: 使用screen.update()手動(dòng)刷新畫(huà)面
  • 定時(shí)延遲: 使用time.sleep()控制循環(huán)速率,減輕CPU負(fù)擔(dān)
  • 異常處理: 使用try-except捕獲可能的異常,確保程序優(yōu)雅退出
  • 入口點(diǎn)檢查: 使用if __name__ == "__main__":確保直接運(yùn)行時(shí)才執(zhí)行主函數(shù)

完整代碼

import turtle
import time

# 設(shè)置窗口和基本參數(shù)
screen = turtle.Screen()
screen.title("簡(jiǎn)易計(jì)時(shí)器/秒表")
screen.bgcolor("black")
screen.setup(width=500, height=300)
screen.tracer(0)  # 關(guān)閉自動(dòng)刷新

# 創(chuàng)建顯示時(shí)間的畫(huà)筆
time_display = turtle.Turtle()
time_display.hideturtle() # 隱藏turtle的箭頭圖標(biāo),使繪圖時(shí)只顯示圖形而不顯示箭頭
time_display.color("cyan") # 設(shè)置turtle繪圖的顏色
time_display.penup()   #抬起turtle的筆,這樣移動(dòng)turtle時(shí)不會(huì)繪制線條
time_display.goto(0, 30)  # 將turtle移動(dòng)到坐標(biāo)(0, 30)的位置,準(zhǔn)備從該點(diǎn)開(kāi)始繪圖或顯示文本

# 創(chuàng)建狀態(tài)顯示的畫(huà)筆
status_display = turtle.Turtle()
status_display.hideturtle()
status_display.speed(0)
status_display.color("white")
status_display.penup()
status_display.goto(0, -30)

# 創(chuàng)建操作提示的畫(huà)筆
help_display = turtle.Turtle()
help_display.hideturtle()
help_display.speed(0)
help_display.color("yellow")
help_display.penup()
help_display.goto(0, -80)
help_display.write("空格鍵: 開(kāi)始/暫停 | R鍵: 重置", align="center", font=("Arial", 14, "normal"))

# 初始化秒表變量
is_running = False
start_time = 0
accumulated_time = 0


# 更新時(shí)間顯示
def update_time_display(elapsed_time):
    hours = int(elapsed_time / 3600)
    minutes = int((elapsed_time % 3600) / 60)
    seconds = int(elapsed_time % 60)
    milliseconds = int((elapsed_time * 1000) % 1000)

    time_str = f"{hours:02d}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}"
    time_display.clear()
    time_display.write(time_str, align="center", font=("Arial", 40, "bold"))


# 更新?tīng)顟B(tài)顯示
def update_status():
    status_display.clear()
    if is_running:
        status = "正在計(jì)時(shí)..."
    else:
        if accumulated_time > 0:
            status = "已暫停"
        else:
            status = "按空格鍵開(kāi)始計(jì)時(shí)"
    status_display.write(status, align="center", font=("Arial", 20, "normal"))


def toggle_timer():
    global is_running, start_time, accumulated_time

    if is_running:
        # 暫停計(jì)時(shí)
        is_running = False
        accumulated_time += time.time() - start_time
    else:
        # 開(kāi)始計(jì)時(shí)
        is_running = True
        start_time = time.time()

    update_status()

def reset_timer():
    global is_running, start_time, accumulated_time
    is_running = False
    accumulated_time = 0
    start_time = 0
    update_status()
    update_time_display(0)
# 設(shè)置鍵盤(pán)事件
screen.listen()
screen.onkeypress(toggle_timer, "space")  # 空格鍵開(kāi)始/暫停
screen.onkeypress(reset_timer, "r")       # R鍵重置


def main():
    # 初始顯示
    update_status()
    update_time_display(0)

    while True:
        if is_running:
            elapsed_time = accumulated_time + (time.time() - start_time)
        else:
            elapsed_time = accumulated_time

        update_time_display(elapsed_time)
        screen.update()
        time.sleep(0.01)  # 小延遲減輕CPU負(fù)擔(dān)


# 啟動(dòng)程序
if __name__ == "__main__":
    try:
        main()
    except:
        print("程序已退出")
    turtle.done() 

效果:

到此這篇關(guān)于Python使用Turtle實(shí)現(xiàn)精確計(jì)時(shí)工具的文章就介紹到這了,更多相關(guān)Python計(jì)時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python+webdriver自動(dòng)化環(huán)境搭建步驟詳解

    python+webdriver自動(dòng)化環(huán)境搭建步驟詳解

    在本篇文章里小編給大家分享了關(guān)于python+webdriver自動(dòng)化環(huán)境搭建的詳細(xì)步驟以及注意點(diǎn),需要的朋友們參考下。
    2019-06-06
  • python兩種注釋用法的示例

    python兩種注釋用法的示例

    這篇文章主要介紹了python兩種注釋用法的示例,幫助大家開(kāi)始學(xué)習(xí)和使用python 注釋?zhuān)信d趣的朋友可以了解下
    2020-10-10
  • Python如何使用Gitlab API實(shí)現(xiàn)批量的合并分支

    Python如何使用Gitlab API實(shí)現(xiàn)批量的合并分支

    這篇文章主要介紹了Python如何使用Gitlab API實(shí)現(xiàn)批量的合并分支,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Python的time模塊中的常用方法整理

    Python的time模塊中的常用方法整理

    這篇文章主要介紹了Python的time模塊中的常用方法整理,time模塊是專(zhuān)門(mén)用于處理日期時(shí)間的模塊,需要的朋友可以參考下
    2015-06-06
  • python直接獲取API傳遞回來(lái)的參數(shù)方法

    python直接獲取API傳遞回來(lái)的參數(shù)方法

    今天小編就為大家分享一篇python直接獲取API傳遞回來(lái)的參數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Python3利用openpyxl讀寫(xiě)Excel文件的方法實(shí)例

    Python3利用openpyxl讀寫(xiě)Excel文件的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于Python3利用openpyxl讀寫(xiě)Excel文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Pytho爬蟲(chóng)中Requests設(shè)置請(qǐng)求頭Headers的方法

    Pytho爬蟲(chóng)中Requests設(shè)置請(qǐng)求頭Headers的方法

    這篇文章主要介紹了Pytho爬蟲(chóng)中Requests設(shè)置請(qǐng)求頭Headers的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Tensorflow、Keras與Python版本兼容性完全解析

    Tensorflow、Keras與Python版本兼容性完全解析

    在深度學(xué)習(xí)的開(kāi)發(fā)過(guò)程中,TensorFlow和Keras作為最流行的深度學(xué)習(xí)框架,已經(jīng)成為了眾多開(kāi)發(fā)者和研究人員的首選,這篇文章主要介紹了Tensorflow、Keras與Python版本兼容性的相關(guān)資料,需要的朋友可以參考下
    2025-11-11
  • Python+Pygame實(shí)現(xiàn)接小彈珠游戲

    Python+Pygame實(shí)現(xiàn)接小彈珠游戲

    這篇文章主要為大家詳細(xì)介紹了Python如何利用Pygame實(shí)現(xiàn)接小彈珠游戲,即用擋板接住會(huì)反彈的小球,隨著次數(shù)的增多,速度變快,分?jǐn)?shù)增多,感興趣的可以了解一下
    2022-12-12
  • 詳解python里使用正則表達(dá)式的全匹配功能

    詳解python里使用正則表達(dá)式的全匹配功能

    這篇文章主要介紹了詳解python里使用正則表達(dá)式的全匹配功能的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下
    2017-10-10

最新評(píng)論

嵩明县| 宁河县| 高碑店市| 巴彦淖尔市| 济阳县| 十堰市| 伊春市| 迁西县| 嘉定区| 泽州县| 珲春市| 太原市| 保山市| 博罗县| 太和县| 桓仁| 宜川县| 罗平县| 梨树县| 香港 | 宁德市| 衡东县| 建平县| 喀喇| 武宁县| 澄城县| 安仁县| 三江| 合山市| 商南县| 竹溪县| 鲁甸县| 林州市| 玉屏| 三亚市| 独山县| 英吉沙县| 双峰县| 罗平县| 天柱县| 曲水县|