windows-python-运动倒计时程序
程序中使用到的部分库的使用方法会在之后的文章中更新
程序预览
涉及的库
- tkinter
- win32
- win32.lib
- xlrd == 1.2.01
其中库1 2 3都不需要安装
程序使用方法
将带有运动信息的excel文件与程序放到同一目录然后运行即可,文件格式如下
本程序后续可能会更新的内容
- 简单计时
- 开始与暂停
- 一项运动开始与结束时通过语音提醒
- 侧面显示运动项目目录
- 加入动作引导图片
总体程序(若有可改进的地方欢迎评论)
from tkinter import *
from win32 import win32api, win32gui, win32print
from win32.lib import win32con
import xlrd
def start1():
"""
倒计时程序
"""
global index
global sporttime
global resttime
global temp
global nextresttime
global sportname
global nextsportname
global nextsporttime
global x
global stoplabel
if stoplabel == 1:
if temp == 0:
sporttime = int(sum[index][1])
resttime = int(sum[index][2])
nextresttime = int(sum[index + 1][2])
sportname = sum[index][0]
nextsportname = sum[index + 1][0]
nextsporttime = int(sum[index + 1][1])
temp = 1
if resttime >= 0:
if sporttime >= 0:
var2.set(resttime)
var1.set(sporttime) # 获取当前时间
var3.set(sportname)
root.after(1000, start1)
sporttime -= 1
return 0
else:
var2.set(resttime) # 获取当前时间
root.after(1000, start1)
resttime -= 1
return 0
else:
index += 1
sporttime = int(sum[index][1])
resttime = int(sum[index][2])
nextresttime = int(sum[index + 1][2])
sportname = sum[index][0]
nextsportname = sum[index + 1][0]
nextsporttime = int(sum[index + 1][1])
elif index < x:
if resttime >= 0:
if sporttime >= 0:
var2.set(resttime)
var1.set(sporttime) # 获取当前时间
if sporttime == 0:
var3.set(nextsportname)
var1.set(nextsporttime)
root.after(1000, start1)
sporttime -= 1
return 0
else:
var2.set(resttime) # 获取当前时间
if resttime == 0:
var2.set(nextresttime)
root.after(1000, start1)
resttime -= 1
return 0
else:
index += 1
sporttime = int(sum[index][1])
resttime = int(sum[index][2])
sportname = sum[index][0]
try:
nextresttime = int(sum[index + 1][2])
nextsportname = sum[index + 1][0]
nextsporttime = int(sum[index + 1][1])
except:
nextresttime = 0
nextsportname = '运动结束'
nextsporttime = 0
var2.set(resttime)
var1.set(sporttime) # 获取当前时间
root.after(0, start1)
return 0
else:
var1.set(0)
var2.set(0)
var3.set('运动结束')
root.after(0, start1)
else:
pass
def start():
"""
将暂停标志stoplabel置为1,3秒后进入计时状态
"""
global stoplabel
stoplabel = 1
var3.set('3s后继续运动')
root.after(3000, start1)
def stop():
"""
将暂停标志stoplabel置为0,同时立即进入暂停状态
"""
global stoplabel
stoplabel = 0
root.after(0, stop1)
def stop1():
"""
进入暂停界面,保持当前状态
"""
global stoplabel
if stoplabel == 0:
var1.set(sporttime)
var2.set(resttime)
var3.set('暂停')
root.after(1000, stop1)
return 0
else:
pass
# 读取运动数据
wb = xlrd.open_workbook(filename=r'.\sum.xlsx')
sheet1 = wb.sheet_by_index(0)
count = 0
x = 1
sum = []
templist = []
totaltime = int(sheet1.row(0)[3].value)
try:
while 1:
for y in range(0, 3):
if sheet1.row(x)[y].value == '':
break
templist.append(sheet1.row(x)[y].value)
sum.append(templist)
templist = []
x += 1
except:
pass
# 初始化变量
screenwidth = win32print.GetDeviceCaps(win32gui.GetDC(0), win32con.DESKTOPHORZRES) # 屏幕横向分辨率
screenheight = win32print.GetDeviceCaps(win32gui.GetDC(0), win32con.DESKTOPVERTRES) # 屏幕纵向分辨率
temp = 0
index = 0
stoplabel = 1
root = Tk()
root.title('运动计时')
root.geometry(f'{str(screenwidth)}x{str(screenheight)}')
sporttime = 0
resttime = 0
nextresttime = 0
sportname = ''
nextsportname = ''
nextsporttime = 0
var1 = StringVar()
var2 = StringVar()
var3 = StringVar()
# 开始与暂停按钮
Button(root, text='开始', bg='#d3fbfb', fg='red', font=('华文新魏', int(screenwidth*0.02)), relief=RAISED, command=start).place(relx=0.001, rely=0.001, height=int(screenwidth*0.04), width=int(screenwidth*0.04*1.6))
Button(root, text='暂停', bg='#d3fbfb', fg='red', font=('华文新魏', int(screenwidth*0.02)), relief=RAISED, command=stop).place(relx=0.001, rely=0.1, height=int(screenwidth*0.04), width=int(screenwidth*0.04*1.6))
# 当前运动计时
timerA = Label(root, textvariable=var1, bg='#AFEEEE', fg='blue', font=("黑体", int(screenwidth*0.2)))
timerA.place(relx=0.17, rely=0.3, height=int(screenwidth*(0.15/0.6)), width=int(screenwidth*(0.15/0.6)*1.6))
# 当前休息计时
timerB = Label(root, textvariable=var2, bg='#AFEEEE', fg='green', font=("黑体", int(screenwidth*0.1)))
timerB.place(relx=0.7, rely=0.58, height=int(screenwidth*(0.075/0.6)), width=int(screenwidth*(0.075/0.6)*1.6))
# 当前运动名称
nameA = Label(root, textvariable=var3, bg='#AFEEEE', fg='blue', font=("黑体", int(screenwidth*0.05)))
nameA.place(relx=0.17, rely=0.1, height=int(screenwidth*(0.05/0.6)), width=int(screenwidth*(0.05/0.6)*5))
# 休息时间标签
nameB = Label(root, text='休息时间', bg='#AFEEEE', fg='green', font=("黑体", int(screenwidth*0.025)))
nameB.place(relx=0.7, rely=0.48, height=int(screenwidth*(0.025/0.6)), width=int(screenwidth*(0.025/0.6)*5))
# 初始界面设置
var2.set('0')
var1.set('0') # 获取当前时间
var3.set('运动未开始')
# 保持窗口不退出
root.mainloop()
xlrd版本过高将无法读取xlsx文件 ↩︎