健身训练计时器(2-2)-程序实现

该博客介绍了使用Python的PyQt5库创建GUI应用,结合Threading线程和pyttsx3语音包实现训练计划的语音播报。程序能够根据用户输入的训练项目、次数和时间进行计时,并在训练和休息阶段通过语音提醒用户。同时,通过线程控制确保UI的响应性和语音的流畅播报。
摘要由CSDN通过智能技术生成

主要知识点:线程Threading,语音播报 pyttsx3 (测试了系统语音包,没找到速度控制,放弃了)

# _*_ coding:utf-8 _*_
# @Time: 2021/10/5  16:48


from PyQt5 import QtWidgets
from Training_GUI import Ui_Form
# import win32com.client   #系统语音包
import pyttsx3  # 语音包

import threading  # 线程
import time


class MyWindow(QtWidgets.QWidget, Ui_Form):
    def __init__(self, total_training_time=0.0, total_rest_time=0.0):
        super(MyWindow, self).__init__()
        self.setupUi(self)
        # 初始化训练项目、次数、时间列表
        self.training_item = [self.lineEdit_Item_1.text(), self.lineEdit_Item_2.text(), self.lineEdit_Item_3.text(),
                              self.lineEdit_Item_4.text(),
                              self.lineEdit_Item_5.text(), self.lineEdit_Item_6.text(), self.lineEdit_Item_7.text()]
        self.training_time = [float(self.lineEdit_Training_Time_1.text()), float(self.lineEdit_Training_Time_2.text()),
                              float(self.lineEdit_Training_Time_3.text()), float(self.lineEdit_Training_Time_4.text()),
                              float(self.lineEdit_Training_Time_5.text()), float(self.lineEdit_Training_Time_6.text()),
                              float(self.lineEdit_Training_Time_7.text())]
        self.training_num = [int(self.lineEdit_Training_Num_1.text()), int(self.lineEdit_Training_Num_2.text()),
                             int(self.lineEdit_Training_Num_3.text()), int(self.lineEdit_Training_Num_4.text()),
                             int(self.lineEdit_Training_Num_5.text()), int(self.lineEdit_Training_Num_6.text()),
                             int(self.lineEdit_Training_Num_7.text())]
        self.rest_time = [float(self.lineEdit_Rest_Time_1.text()), float(self.lineEdit_Rest_Time_2.text()),
                          float(self.lineEdit_Rest_Time_3.text()), float(self.lineEdit_Rest_Time_4.text()),
                          float(self.lineEdit_Rest_Time_5.text()), float(self.lineEdit_Rest_Time_6.text()),
                          float(self.lineEdit_Rest_Time_7.text())]
        self.now_training_item = self.next_training_item = self.lineEdit_Item.text()
        self.now_training_num = self.next_training_num = self.lineEdit_Num.text()
        self.now_training_time = self.next_training_time = self.lineEdit_TrainingTime.text()
        self.now_rest_time = self.next_rest_time = self.lineEdit_RestTime.text()
        self.training_flag = False  # 开始训练标识
        self.rest_flag = False  # 开始休息标识
        # 初始化总时间
        for index in range(len(self.training_time)):
            total_training_time += self.training_time[index] * self.training_num[index]
            total_rest_time += self.rest_time[index] * self.training_num[index]

        self.lineEdit_Training_Time_Total.setText(str(total_training_time))
        self.lineEdit_Rest_Time_Total.setText(str(total_rest_time))

        self.engine = pyttsx3.init()  # 创建语音对象
        rate = self.engine.getProperty('rate')
        self.engine.setProperty('rate', rate + 50)  # 调节语速

        self.thread1 = threading.Thread(target=self.training_start)  # 创建计时线程1
        self.thread2 = threading.Thread(target=self.reading_start)  # 创建发音线程2
        self.thread1.setDaemon(True)  # 设为守护线程,主程序关闭,这本线程自动关闭
        self.thread2.setDaemon(True)
        # 创建信号和槽
        self.pushButton_Start.clicked.connect(self.threading1_start)
        self.pushButton_Pause.clicked.connect(self.training_pause)
        self.pushButton_Stop.clicked.connect(self.training_stop)
        # self.lineEdit_Num.textChanged.connect(self.say_num)
        # self.lineEdit_TrainingTime.textChanged.connect(self.say_time)
        # self.lineEdit_Item.textChanged.connect(self.say_item)
        # 设置按钮不可见
        self.pushButton_Pause.hide()



    def threading1_start(self):
        self.pushButton_Start.setEnabled(False)
        self.thread1.start()
        self.thread2.start()

    def reading_start(self):
        while True:
            if self.now_training_item != self.next_training_item:
                self.next_training_item = self.now_training_item
                self.engine.say("%s " % self.now_training_item)
                # self.engine.runAndWait()
            if self.now_training_num != self.next_training_num:
                self.next_training_num = self.now_training_num
                self.engine.say("第 %s 组" % self.now_training_num)
                # self.engine.runAndWait()
            if self.training_flag:
                self.engine.say("开始训练")
                self.training_flag = False
            if self.now_training_time != self.next_training_time:
                self.next_training_time = self.now_training_time
                self.engine.say(" %s " % self.now_training_time)
                # self.engine.runAndWait()
            if self.rest_flag:
                self.engine.say("开始休息")
                self.rest_flag = False
            if self.now_rest_time != self.next_rest_time:
                self.next_rest_time = self.now_rest_time
                self.engine.say(" %s " % self.now_rest_time)
                # self.engine.runAndWait()
            self.engine.runAndWait()


    def training_start(self):
        for item in range(len(self.training_time)):
            self.lineEdit_Item.setText(self.training_item[item])
            self.now_training_item = self.training_item[item]
            time.sleep(1)
            for num in range(1, self.training_num[item] + 1, 1):
                self.lineEdit_Num.setText(str(num))
                self.now_training_num = str(num)
                seconds = int(self.training_time[item] * 60)
                self.training_flag = True
                time.sleep(1)
                for second in range(seconds - 1, -1, -1):
                    self.lineEdit_TrainingTime.setText(str(second))
                    self.now_training_time = str(second)
                    time.sleep(1)
                seconds = int(self.rest_time[item] * 60)
                self.rest_flag = True
                for second in range(seconds - 1, -1, -1):
                    self.lineEdit_RestTime.setText(str(second))
                    self.now_rest_time = str(second)
                    time.sleep(1)

    def training_pause(self):
        pass

    def training_stop(self):
        myShow.close()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    myShow = MyWindow()
    myShow.show()
    sys.exit(app.exec_())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值