代码
import time
import PySimpleGUI as sg
# 初始化变量
start_time = 0
elapsed_time = 0
is_running = False
layout = [
[sg.T('00', key='-H-', font=(None, 72)), sg.T(":", font=(None, 72)),
sg.T('00', key='-M-', font=(None, 72)), sg.T(":", font=(None, 72)),
sg.T('00', key='-S-', font=(None, 72)), sg.T(".", font=(None, 72)),
sg.T('0', key='-TENTH-', font=(None, 72))],
[sg.B("开始", key='-START-', font=(None, 36)),
sg.B("暂停", key='-STOP-', font=(None, 36)),
sg.B("重置", key='-RESET-', font=(None, 36))]
]
window = sg.Window('高精度秒表', layout, finalize=True)
while True:
event, values = window.read(timeout=10) # 短超时以减少延迟
if event == sg.WIN_CLOSED:
break
if event == '-START-' and not is_running:
start_time = time.time() - elapsed_time # 调整开始时间以继续计时
is_running = True
if event == '-STOP-' and is_running:
elapsed_time = time.time() - start_time # 记录已过时间
is_running = False
if event == '-RESET-':
is_running = False
elapsed_time = 0
window['-H-'].update('00')
window['-M-'].update('00')
window['-S-'].update('00')
window['-TENTH-'].update('0')
# 更新显示(即使暂停也显示当前时间)
if is_running or elapsed_time > 0:
current_time = time.time() - start_time if is_running else elapsed_time
tenths = int(current_time * 10) % 10
seconds = int(current_time) % 60
minutes = int(current_time // 60) % 60
hours = int(current_time // 3600)
window['-H-'].update(f'{hours:02d}')
window['-M-'].update(f'{minutes:02d}')
window['-S-'].update(f'{seconds:02d}')
window['-TENTH-'].update(str(tenths))
window.close()
在运行代码之前,应该在命令提示符中以管理员权限运行命令安装图形化模块:
pip install PySimpleGUI
运行效果
exe文件
exe文件:python高精度秒表(exe)资源-CSDN下载https://download.csdn.net/download/w181078/91610650