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.
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.
This project can be broken down into 4 main steps:
- Import modules
- Create the Tkinter GUI
- Create the GUI’s menus, buttons, and frames
- Define the alarm function
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:
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:
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:
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.
We will also set the default hour value to 0 until the user selects a different time. The code for this step will be:
Hint: You may want to format the widget to make sure it’s in the correct location. To do this, you can type:
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:
Finally, we’ll just add a “set alarm button using this line of code:
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:
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.
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.
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:
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.