今天來補充碼表可以添加的功能(想加就加)
A. 一鍵把圈速複製到剪貼簿
# 插在 GUI 區塊附近,新增按鈕:
ttk.Button(btns, text="複製圈速", command=lambda: copy_laps()).grid(row=0, column=3, padx=4)
# 在檔案尾端附近加上:
def copy_laps():
items = lap_list.get(0, tk.END)
text = "\n".join(reversed(items)) # 由舊到新
root.clipboard_clear()
root.clipboard_append(text)
實作:
B. 匯出圈速為 CSV
# 匯出按鈕
from tkinter import filedialog
ttk.Button(btns, text="匯出 CSV", command=lambda: export_csv()).grid(row=0, column=4, padx=4)
def export_csv():
import csv
path = filedialog.asksaveasfilename(defaultextension=".csv", filetypes=[("CSV","*.csv")], initialfile="laps.csv")
if not path: return
rows = [("Lap","LapTime","Total")]
# Listbox 顯示是「新到舊」,輸出時改成「舊到新」
for i, line in enumerate(reversed(lap_list.get(0, tk.END)), 1):
# 解析格式:Lap XX 單圈 mm:ss.cc 累計 mm:ss.cc
parts = line.split()
lap = i
lap_time = parts[3]
total = parts[5]
rows.append((lap, lap_time, total))
with open(path, "w", newline="", encoding="utf-8") as f:
csv.writer(f).writerows(rows)

打包成單一執行檔(選用)
想給別人直接用(免裝 Python):
pip install pyinstaller
pyinstaller --noconsole --onefile stopwatch_gui.py
# 產物在 dist/stopwatch_gui

今日小結