📜  创建一个Python脚本通知休息一下

📅  最后修改于: 2022-05-13 01:55:48.911000             🧑  作者: Mango

创建一个Python脚本通知休息一下

当我们使用笔记本电脑或 PC 时,我们通常不会休息。它可能会影响我们的视力和思想。因此,使用Python,我们可以编写一个程序来通知我们在用户再次开始在笔记本电脑上工作的某个时间后我们必须重新开始。

需要的模块

  • pyttsx3 -是Python中的文本到语音转换库。应用程序调用 pyttsx3.init() 工厂函数来获取对 pyttsx3 的引用。这是一个非常易于使用的工具,可以将输入的文本转换为语音。要安装此模块,请在终端中键入以下命令。
pip install pyttsx3
  • plyer – Plyer 模块用于访问硬件的功能。这个模块没有内置在Python中。我们需要在外部安装它。要安装此模块,请在终端中键入以下命令。
pip install plyer 

当我们启动我们的笔记本电脑或 PC 时,我们只需要运行Python程序,我们将安排操作,就像每 50 分钟我们的 PC 或笔记本电脑会向我们发出通知并告诉我们休息一下。很多时候,我们只是忽略了给我们的通知,但是用笔记本电脑说话,我们会帮助我们提醒我们必须休息一下。

创建说话方法:

Python3
import pyttsx3
from plyer import notification
import time
  
  
# Speak method
def Speak(self, audio):
    
    # Calling the initial constructor 
    # of pyttsx3
    engine = pyttsx3.init('sapi5')
      
    # Calling the getter method
    voices = engine.getProperty('voices')
      
    # Calling the setter method
    engine.setProperty('voice', voices[1].id)
       
    engine.say(audio)
    engine.runAndWait()


Python3
def Take_break():
      
    Speak("Do you want to start sir?")
    question = input()
      
    if "yes" in question:
        Speak("Starting Sir")
      
    if "no" in question:
        Speak("We will automatically start after 5 Mins Sir.")
        time.sleep(5*60)
        Speak("Starting Sir")
      
    # A notification we will held that 
    # Let's Start sir and with a message of
    # will tell you to take a break after 45
    # mins for 10 seconds
    while(True):
        notification.notify(title="Let's Start sir",
        message="will tell you to take a break after 45 mins",
        timeout=10)
          
        # For 45 min the will be no notification but 
        # after 45 min a notification will pop up.
        time.sleep(0.5*60)
  
        Speak("Please Take a break Sir")
          
        notification.notify(title="Break Notification",
        message="Please do use your device after sometime as you have"
        "been continuously using it for 45 mins and it will affect your eyes",
        timeout=10)
  
          
# Driver's Code        
if __name__ == '__main__':
    Take_break()


Python3
import pyttsx3
from plyer import notification
import time
  
  
# Speak method
def Speak(self, audio):
    
    # Calling the initial constructor 
    # of pyttsx3
    engine = pyttsx3.init('sapi5')
      
    # Calling the getter method
    voices = engine.getProperty('voices')
      
    # Calling the setter method
    engine.setProperty('voice', voices[1].id)
       
    engine.say(audio)
    engine.runAndWait()
      
      
def Take_break():
      
    Speak("Do you want to start sir?")
    question = input()
      
    if "yes" in question:
        Speak("Starting Sir")
      
    if "no" in question:
        Speak("We will automatically start after 5 Mins Sir.")
        time.sleep(5*60)
        Speak("Starting Sir")
      
    # A notification we will held that 
    # Let's Start sir and with a message of
    # will tell you to take a break after 45
    # mins for 10 seconds
    while(True):
        notification.notify(title="Let's Start sir",
        message="will tell you to take a break after 45 mins",
        timeout=10)
          
        # For 45 min the will be no notification but 
        # after 45 min a notification will pop up.
        time.sleep(0.5*60)
  
        Speak("Please Take a break Sir")
          
        notification.notify(title="Break Notification",
        message="Please do use your device after sometime as you have"
        "been continuously using it for 45 mins and it will affect your eyes",
        timeout=10)
  
          
# Driver's Code        
if __name__ == '__main__':
    Take_break()


创建 Take_break 方法,该方法将为我们的窗口创建弹出通知

Python3

def Take_break():
      
    Speak("Do you want to start sir?")
    question = input()
      
    if "yes" in question:
        Speak("Starting Sir")
      
    if "no" in question:
        Speak("We will automatically start after 5 Mins Sir.")
        time.sleep(5*60)
        Speak("Starting Sir")
      
    # A notification we will held that 
    # Let's Start sir and with a message of
    # will tell you to take a break after 45
    # mins for 10 seconds
    while(True):
        notification.notify(title="Let's Start sir",
        message="will tell you to take a break after 45 mins",
        timeout=10)
          
        # For 45 min the will be no notification but 
        # after 45 min a notification will pop up.
        time.sleep(0.5*60)
  
        Speak("Please Take a break Sir")
          
        notification.notify(title="Break Notification",
        message="Please do use your device after sometime as you have"
        "been continuously using it for 45 mins and it will affect your eyes",
        timeout=10)
  
          
# Driver's Code        
if __name__ == '__main__':
    Take_break()

以下是完整代码

Python3

import pyttsx3
from plyer import notification
import time
  
  
# Speak method
def Speak(self, audio):
    
    # Calling the initial constructor 
    # of pyttsx3
    engine = pyttsx3.init('sapi5')
      
    # Calling the getter method
    voices = engine.getProperty('voices')
      
    # Calling the setter method
    engine.setProperty('voice', voices[1].id)
       
    engine.say(audio)
    engine.runAndWait()
      
      
def Take_break():
      
    Speak("Do you want to start sir?")
    question = input()
      
    if "yes" in question:
        Speak("Starting Sir")
      
    if "no" in question:
        Speak("We will automatically start after 5 Mins Sir.")
        time.sleep(5*60)
        Speak("Starting Sir")
      
    # A notification we will held that 
    # Let's Start sir and with a message of
    # will tell you to take a break after 45
    # mins for 10 seconds
    while(True):
        notification.notify(title="Let's Start sir",
        message="will tell you to take a break after 45 mins",
        timeout=10)
          
        # For 45 min the will be no notification but 
        # after 45 min a notification will pop up.
        time.sleep(0.5*60)
  
        Speak("Please Take a break Sir")
          
        notification.notify(title="Break Notification",
        message="Please do use your device after sometime as you have"
        "been continuously using it for 45 mins and it will affect your eyes",
        timeout=10)
  
          
# Driver's Code        
if __name__ == '__main__':
    Take_break()

输出:

注意:还会生成语音命令。