SIZ Tutorials | ALARM CLOCK IN PYTHON | by @sheham-sh

in Steem Infinity Zone3 years ago

Hi Steemians, hope you're doing good. In this tutorial I'll be sharing python code to create graphical Alarm Clock so lets dive deeper into it.


Alarm Clock.png


siz2.png

About Project

The goal of this project is to create an alarm clock in Python. Python includes some very innovative libraries, such as datetime and tkinter, that allow us to build the project using the current date and time, as well as provide a user interface for setting the alarm in 24-hour format.
From here we'll go step by step.

Step 1: Importing all the necessary libraries to form the alarm clock:

Tkinter module belongs to a standard library of GUI in Python. It helps us to create a dialog box with any information that we want to provide or get from the users.

Datetime and time modules in python help us to work with the dates and time of the current day when the user is operating python and to manipulate it too.

Winsound module provides access to the basic sound playing machinery provided by Windows platforms. This is useful to generate the sound immediately when a function is called.

from tkinter import *
import datetime
import time
import winsound 


siz2.png

Step 2

Define a function named as alarm() which takes the argument of (set_alarm_timer).It contains a while loop with a Boolean function True which makes the program automatic to work. time.sleep(1) halts the execution of the further commands given until we get the time value from the user later in the code and returns the background thread of the clock time going on at a regular interval. Get the current time using current_time which takes the argument of datetime.datetime.now(), .now is used to print the time and date is used to print the current date by string conversion using strftime().

Define another function here named actual_time() which takes in the user value for setting the alarm in the string format. The same argument of (set_alarm_timer) as alarm before to execute the while loop which we further use while making GUI. If loop suggests that if the user input time set_alarm_timer matches with the while loop ongoing time now, the message is printed as ”Time to Wake up” winsound.SND_ASYNC plays the system generated sound as soon the condition satisfies, acting as a reminder for the alarm clock.

def alarm(set_alarm_timer):
    while True:
        time.sleep(1)
        current_time = datetime.datetime.now()
        now = current_time.strftime("%H:%M:%S")
        date = current_time.strftime("%d/%m/%Y")
        print("The Set Da

te is:",date)
print(now)
if now == set_alarm_timer:
print("Time to Wake up")
winsound.PlaySound("sound.mp3",winsound.SND_ASYNC)
break

def actual_time():
    set_alarm_timer = f"{hour.get()}:{min.get()}:{sec.get()}"
    alarm(set_alarm_timer)


siz2.png

Step 3; Creating GUI using tkinter

To Initialize tkinter, we pass a command under the name clock as Tk(). The dialog box has the title as DataFlair Alarm Clock with a geometry of (400*200). We pass on the heading to mention the time format for 24 hours
using time_format.
The second heading is given above the user input boxes for the labeling to be “Hour Min Sec” using addTime.
Just to make the dialog box look funkier, adding another label as “when to wake you up” using setYourAlarm. As we have already converted the current time in the string before (actual time), the variables we initialize for the user input dialog boxes are in StringVar(). Finally make the input boxes such as hourTime, minTime, and secTime which takes the entry of the time the user wants to set the alarm on in 24-hour format. Submit takes the command of the defined function actual_time and executes the clock as it acts as a set button to start the program.
Clock.mainloop() is the basic and the last command was given to compile all the previous commands with their basic settings of color, font, width, axis, etc. and displays the window as soon as the program is run.

clock = Tk()
clock.title("Alarm Clock")
clock.geometry("400x200")

time_format=Label(clock, text= "Enter time in 24 hour format!",
fg="red",bg="black",font="Arial").place(x=60,y=120)
addTime = Label(clock,text = "Hour Min Sec",font=60).place(x = 110)
setYourAlarm = Label(clock,text = "When to wake you up",fg="blue",relief =
"solid",font=("Helevetica",7,"bold")).place(x=0, y=29)

# The Variables we require to set the alarm(initialization):
hour = StringVar()
min = StringVar()
sec = StringVar()

# Time required to set the alarm clock:
hourTime= Entry(clock,textvariable = hour,bg = "pink",width =15).place(x=110,y=30)
minTime= Entry(clock,textvariable = min,bg = "pink",width = 15).place(x=150,y=30)
secTime = Entry(clock,textvariable = sec,bg = "pink",width = 15).place(x=200,y=30)

# To take the time input by user:
submit = Button(clock,text = "Set Alarm",fg="red",width = 10,command =
actual_time).place(x =110,y=70)
clock.mainloop()


siz2.png

Ouput


1.png


2.png


3.png


siz2.png

This post took my hours of time so give it an upvote and keep supporting.
Thanks


Achievement 1
Achievement 2

Divider 2.png

Steem Infinity Zone Team
@cryptokraze | @arie.steem | @qasimwaqar | @vvarishayy | @suboohi

Footer.png

Click Here to Join Official SIZ Discord Channel

Discord
Twitter
Facebook

Divider 2.png

Sort:  
 3 years ago 

I'll definitely try this code. It looks simple and interesting

Yeah sure👍

Python is advanced level language. Keep it up.

Yes it is, especially python libraries☺️

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.030
BTC 58269.26
ETH 3067.65
USDT 1.00
SBD 2.25