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

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

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

功能特點(diǎn)

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

支持開始/暫停計時(空格鍵)

支持重置計時(R鍵)

狀態(tài)顯示(正在計時/已暫停/按空格鍵開始計時)

清晰的操作提示

使用方法

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

按下空格鍵開始計時

再次按下空格鍵暫停計時

按下R鍵可以重置計時器回到00:00:00.000

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

1.核心模塊:

turtle: 提供跨平臺的圖形化界面繪制功能

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

2.設(shè)計模式:

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

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

3.算法原理:

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

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

代碼詳解

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

import turtle
import time

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

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

# 創(chuàng)建狀態(tài)顯示的畫筆
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)建操作提示的畫筆
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("空格鍵: 開始/暫停 | R鍵: 重置", align="center", font=("Arial", 14, "normal"))

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

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

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

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

# 更新時間顯示
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ài)顯示
def update_status():
    status_display.clear()
    if is_running:
        status = "正在計時..."
    else:
        if accumulated_time > 0:
            status = "已暫停"
        else:
            status = "按空格鍵開始計時"
    status_display.write(status, align="center", font=("Arial", 20, "normal"))

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

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

計時器控制邏輯

def toggle_timer():
    global is_running, start_time, accumulated_time
    
    if is_running:
        # 暫停計時
        is_running = False
        accumulated_time += time.time() - start_time
    else:
        # 開始計時
        is_running = True
        start_time = time.time()
    
    update_status()

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

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

計時器重置功能

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)用知識點(diǎn):

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

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

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

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

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

主循環(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)

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

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

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

完整代碼

import turtle
import time

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

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

# 創(chuàng)建狀態(tài)顯示的畫筆
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)建操作提示的畫筆
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("空格鍵: 開始/暫停 | R鍵: 重置", align="center", font=("Arial", 14, "normal"))

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


# 更新時間顯示
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ài)顯示
def update_status():
    status_display.clear()
    if is_running:
        status = "正在計時..."
    else:
        if accumulated_time > 0:
            status = "已暫停"
        else:
            status = "按空格鍵開始計時"
    status_display.write(status, align="center", font=("Arial", 20, "normal"))


def toggle_timer():
    global is_running, start_time, accumulated_time

    if is_running:
        # 暫停計時
        is_running = False
        accumulated_time += time.time() - start_time
    else:
        # 開始計時
        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è)置鍵盤事件
screen.listen()
screen.onkeypress(toggle_timer, "space")  # 空格鍵開始/暫停
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)


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

效果:

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

相關(guān)文章

  • 如何使用python編寫一個簡單的課時記錄系統(tǒng)

    如何使用python編寫一個簡單的課時記錄系統(tǒng)

    編寫一個應(yīng)用系統(tǒng)需要多方面的知識和技能,下面這篇文章主要給大家介紹了關(guān)于如何使用python編寫一個簡單的課時記錄系統(tǒng)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-04-04
  • Python中的裝飾器使用

    Python中的裝飾器使用

    這篇文章主要介紹了Python中的裝飾器使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Python 旋轉(zhuǎn)打印各種矩形的方法

    Python 旋轉(zhuǎn)打印各種矩形的方法

    今天小編就為大家分享一篇Python 旋轉(zhuǎn)打印各種矩形的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • 詳解Python中l(wèi)ist[::-1]的幾種用法

    詳解Python中l(wèi)ist[::-1]的幾種用法

    這篇文章主要介紹了詳解Python中l(wèi)ist[::-1]的幾種用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 詳解利用Pandas求解兩個DataFrame的差集,交集,并集

    詳解利用Pandas求解兩個DataFrame的差集,交集,并集

    這篇文章主要和大家講解一下如何利用Pandas函數(shù)求解兩個DataFrame的差集、交集、并集,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-07-07
  • Python GUI之tkinter詳解

    Python GUI之tkinter詳解

    今天帶大家學(xué)習(xí)Python GUI之tkinter的相關(guān)知識,文中對如何使用tkinter作了非常詳細(xì)的介紹及代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-10-10
  • Python實現(xiàn)Selenium自動化Page模式

    Python實現(xiàn)Selenium自動化Page模式

    這篇文章主要介紹了Python實現(xiàn)Selenium自動化Page模式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python利用裝飾器實現(xiàn)類似于flask路由

    Python利用裝飾器實現(xiàn)類似于flask路由

    這篇文章主要為大家詳細(xì)介紹了Python如何利用裝飾器實現(xiàn)類似于flask路由,文中的示例代碼講解詳細(xì),對我們深入了解Python有一點(diǎn)的幫助,感興趣的可以了解一下
    2023-02-02
  • 30行Python代碼打造一款簡單的人工語音對話

    30行Python代碼打造一款簡單的人工語音對話

    使用gtts和speech_recognition實現(xiàn)簡單的人工語音對話,通過將語音變成文本,然后文本變成語音,僅用30行代碼,超級簡單,對Python人工語音對話的實現(xiàn)過程及完整代碼感興趣的朋友一起看看吧
    2021-05-05
  • Python實現(xiàn)按學(xué)生年齡排序的實際問題詳解

    Python實現(xiàn)按學(xué)生年齡排序的實際問題詳解

    這篇文章主要給大家介紹了關(guān)于Python實現(xiàn)按學(xué)生年齡排序?qū)嶋H問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08

最新評論