Modules used:
使用的模块:
To create this script, we will use 3 modules:
要创建此脚本,我们将使用3个模块:
datetime
约会时间
time
时间
pyttsx3
pyttsx3
datetime module: datetime is an inbuilt python module which will help us to provide some function which will deal with the date and time, we will use this module just to get the actual standard time.
datetime模块 :datetime是一个内置的python模块,它将帮助我们提供一些处理日期和时间的功能,我们将使用该模块来获取实际的标准时间。
time module: time is an inbuilt python module that provides us with various time-related functions and we are using this module just to sleep the program for 1 second.
time模块 :time是一个内置的python模块,它为我们提供了各种与时间相关的功能,我们正在使用此模块将程序Hibernate1秒钟。
pyttsx3: pyttsx3 is a python module that will help us to convert the input text into speech.
pyttsx3 :pyttsx3是一个python模块,它将帮助我们将输入文本转换为语音。
We can install in 2 ways:
我们可以通过两种方式安装:
Using the command:
使用命令:
pip install pyttsx3
Pycharm users: Go to the project interpreter and install this module from there
Pycharm用户:转到项目解释器并从那里安装此模块
Note: This alarming interface is based on 24 hours clock format.
注意:此警报界面基于24小时时钟格式。
How we will do this?
我们将如何做到这一点?
We have to use a loop for this interface.
我们必须为此接口使用循环。
We will use the datetime module to get the actual standard time.
我们将使用datetime模块获取实际的标准时间。
Now if the users time matched with the standard time then we have to alarm the user, for this, we will use the pyttsx3 module inside an if condition statement and we will apply the various function of pyttsx3 inside the if condition.
现在,如果用户时间与标准时间匹配,那么我们必须警告用户,为此,我们将在if条件语句中使用pyttsx3模块,并在if条件中应用pyttsx3的各种功能。
Now the function that we will in the script are:
现在,我们将在脚本中使用的功能是:
datetime.datetime.now(): This will give us the exact date and standard time.
datetime.datetime.now() :这将为我们提供确切的日期和标准时间。
datetime.datetime.now().strftime("%H:%M"): Here we only work with time, to get the time we will use this function.
datetime.datetime.now()。strftime(“%H:%M”) :在这里,我们仅使用时间,以获取使用此功能的时间。
time.sleep(1): we have to run a loop in every second so we will stop the functioning of the program.
time.sleep(1) :我们必须每秒运行一次循环,以便我们停止程序的运行。
Regarding the pyttsx3:
关于pyttsx3 :
When the standard time matches with the users time then we have to alarm the user with the help of pyttsx3, the functions that we will use are:
当标准时间与用户时间匹配时,我们必须在pyttsx3的帮助下警告用户,我们将使用的功能是:
pyttsx3.init():This function will create a engine and all the other function
pyttsx3.init() :此函数将创建一个引擎以及所有其他函数
engine.say(): This function will convert the text to speech.
engine.say() :此函数会将文本转换为语音。
engine.runAndWait(): This will make the speech audible.
engine.runAndWait() :这将使语音可听。
Program:
程序:
# import libraries(datetime,time,pyttsx3)
import time,pyttsx3
from datetime import datetime
print("Hello, welcome to my alarming interface...")
print("It is based on 24 hours clock format...")
Name = input("Enter your name: ")
# printing the current time
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time is: ", current_time)
# Enter the time(Hours and minute)
Time=input("Enter the time (HH24:MM): ")
# starting the loop
while True:
# Getting the standard time with the
# help of datetime module
Standard_time=datetime.now().strftime("%H:%M")
# sleep the program for about 1 sec
time.sleep(1)
# if condition to check wether the input
# time has matched or not
if Time==Standard_time:
count=0
while count<=10:
count=count+1
# creating a engine for voice mode
engine=pyttsx3.init()
# setting the voice
engine.say("Wake up"+Name)
engine.runAndWait()
print("Thankyou For using the Interface")
break
Output:
输出:
Hello, welcome to my alarming interface...
It is based on 24 hours clock format...
Enter your name: Abhinav
Current Time is: 00:33:38
Enter the time (HH24:MM): 00:35
This is the way we can create an Alarming clock script in python.
这是我们可以在python中创建Alarming Clock脚本的方法。
翻译自: https://www.includehelp.com/python/alarm-clock-in-python-with-the-help-of-datetime-and-pyttsx3.aspx