Python Project for kids: Python Alarm Clock

Table Of Content:

Introduction: 

In this article, we are going to be creating an alarm clock that can be used for a variety of purposes, from waking you up in the morning to alerting you at a specific time. The project will allow users to use a menu to select a time for their alarm to go off, then will alert them at the correct time. Whether you are new to Python or have some prior experience, this project is a great way to learn more about making functional programs and helps you apply your knowledge in a practical and fun way.

alarm

Who is this Project For?

This is an intermediate-level project for those who are new to Python. At this point, you should already know the basics of creating functions, installing packages, and creating simple GUIs (graphical user interfaces) with Tkinter. These are the concepts that you will use and build upon as you code this project.

What Will We Learn?

This project focuses on using the Tkinter library to create the program’s GUI, as well as the datetime, time, and playsound modules to set an alarm and generator notifications. This is a relatively simple project that has a wide variety of uses, so let’s get started!

Features to Consider:

  • The user should be able to select the time they want to be alerted
  • The program will play an alarm sound when the time is up
  • The console will display the current time as well as the alert time so that users can see how long they have left.

Main Steps:

This project can be broken down into 4 main steps: 

  1. Import modules
  2. Create the Tkinter GUI
  3. Create the GUI’s menus, buttons, and frames
  4. Define the alarm function
Python Alarm Clock

Step 1: Import Modules:

The first thing we’ll need to do to begin the project is import all of the libraries and modules that we’ll be using. We will use the Tkinter library for the graphics, and the datetime, time, and playsound modules to create the alarm and sound effects. We will also use threading to allow the computer to run multiple sections of code at once. First, you’ll need to import the libraries we need and initialize the Tkinter window. To do this, we’ll type: 

from tkinter import *
import datetime
import time
import playsound
from threading import *
Hint: If you haven’t already downloaded these packages to your computer you can do it using the command line and the “pip install” command.

Step 2: Create the Tkinter GUI

Now that all of the modules have been imported, it’s time to initialize Tkinter and create the program’s graphical user interface. We’ll start by initializing Tkinter and setting up a window for the alarm. To do this, you will type:

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

Next, we need to use threading so that the program can run multiple sections of code at once. To do this we will create a function called “Threading” and set it up so that the code can handle multiple aspects of the code at the same time in order to make the program work better. This will help us ensure that the timing of the alarm works well and is accurate. The code for this section will be:

def Threading():
	t1=Thread(target=alarm)
	t1.start()

Step 3: Create the GUI’s Menus, Buttons, and Frames 

Now that the GUI is set up, it’s time to move on to add frames, labels, and buttons to the GUI.  We’ll start by creating two simple labels: “Alarm Clock” and “Set Time:”. These labels will be centered in the window and will let users know what the program is and how to interact with it. Next, we’ll create a widget using a StringVar to store all of the hour values that users can choose from. Because the clock uses 24-hour time, these will be the values from 0-24. 

Python Alarm Clock

We will also set the default hour value to 0 until the user selects a different time. The code for this step will be:

Label(root,text="Alarm Clock",font=("Helvetica 20 bold"),fg="red")
.pack(pady=10)
Label(root,text="Set Time:",font=("Helvetica 15 bold")).pack()

frame = Frame(root)
frame.pack()

hour = StringVar(root)
hours = ('00', '01', '02', '03', '04', '05', '06', '07', '08', '09'
, '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20'
, '21', '22', '23', '24')
hour.set(hours[0])

Hint: You may want to format the widget to make sure it’s in the correct location. To do this, you can type:
hrs = OptionMenu(frame, hour, *hours)
hrs.pack(side=LEFT)

Next we’ll make two more widgets- one for the minutes and one for the seconds. These will also be formatted using Tkinter the same way we just formatted the hours. By the end of this step your code should look like this:

minute = StringVar(root)
minutes = ('00', '01', '02', '03', '04', '05', '06', '07',
		'08', '09', '10', '11', '12', '13', '14', '15',
		'16', '17', '18', '19', '20', '21', '22', '23',
		'24', '25', '26', '27', '28', '29', '30', '31',
		'32', '33', '34', '35', '36', '37', '38', '39',
		'40', '41', '42', '43', '44', '45', '46', '47',
		'48', '49', '50', '51', '52', '53', '54', '55',
		'56', '57', '58', '59', '60')
minute.set(minutes[0])
mins = OptionMenu(frame, minute, *minutes)
mins.pack(side=LEFT)

second = StringVar(root)
seconds = ('00', '01', '02', '03', '04', '05', '06', '07',
		'08', '09', '10', '11', '12', '13', '14', '15',
		'16', '17', '18', '19', '20', '21', '22', '23',
		'24', '25', '26', '27', '28', '29', '30', '31',
		'32', '33', '34', '35', '36', '37', '38', '39',
		'40', '41', '42', '43', '44', '45', '46', '47',
		'48', '49', '50', '51', '52', '53', '54', '55',
		'56', '57', '58', '59', '60')
second.set(seconds[0])
secs = OptionMenu(frame, second, *seconds)
secs.pack(side=LEFT)

Python Alarm Clock

Finally, we’ll just add a “set alarm button using this line of code:

Button(root,text="Set Alarm"
,font=("Helvetica 15"),command=Threading).pack(pady=20)

Once this final step is complete, we’re finished with the GUI and it’s time to move on to creating the alarm function!

Step 4: Define the Alarm Function

Now that the GUI has been completed, it’s time to create the alarm function. Start by defining a function called “alarm”, and then creating a while statement. We will use this statement to take input from the hour, minute, and second widgets, and then we’ll use a timer.sleep command to make the program wait 1 second. At this point, the code should look like this:

def alarm():
	while True:to
		set_alarm_time = f"{hour.get()}
:{minute.get()}:{second.get()}"
		time.sleep(1)

Next, we’ll add a line of code to take the computer’s local time and compare it with the set alarm time like so. This will be done using the datetime module.

current_time = datetime.datetime.now().strftime("%H:%M:%S")
		print(current_time,set_alarm_time)

And now there are only a few lines left to code! At this point, we just need to create an if-statement to check if the times are the same, as well as a command to execute Tkinter. Let’s start by creating an if-statement to check if the current time is equal to the alarm time. If so, we want to print “time to wake up”, and play the alarm sound.

Hint: You may need to download an alarm sound and add it to your program’s files. Then, you can simply use the command playsound(“alarmName.wav”) to play the sound. 
Python Alarm Clock

At the very end, we’ll just add a command to execute the Tkinter code and we’re done! Now that the project is finished, the code should be:

if current_time == set_alarm_time:
			print("Time to Wake up")
			playsound('alarm.wav')
root.mainloop()

Project Complete!

how to make alarm python

And now the project is complete! At this point, you will be able to use this alarm clock to alert you about events at specific times or to wake you up after a nap. Feel free to test your code now and see how it works. If you’re stuck or have any issues with your code, try reviewing it again either in your text editor or by looking at the code mentioned above as a reference. 

Geek Team

Geekedu is an expert in Coding and Math learning. Our goal is to inspire and empower youth to use their knowledge of technology to become the influencers, inventors and innovators of the future.

Sign up and get a 60-minute free assessment class

Book A FREE Trial