Build a Basic Alarm Clock in Python with Tkinter
Basic Alarm Clock with GUI:
Build an alarm clock with a graphical user interface.
Input values:
1. User interacts with the graphical user interface (GUI) by setting the alarm time and activating/deactivating the alarm.
2. Users can also snooze or dismiss the alarm when it rings.
Output value:
Visual representation of the alarm clock interface displaying the current time, set alarm time, and feedback on alarm activation/deactivation.
Example:
Input values: 1. Set alarm time: 07:00 AM - User sets the alarm time to 7:00 AM. Output value: Visual representation of the alarm clock interface displays the set alarm time as 07:00 AM. Input values: 2. Activate alarm - User activates the alarm. Output value: Visual representation of the alarm clock interface displays the alarm activated and waits for the set alarm time. Input values: 3. Alarm rings at 07:00 AM - The current time matches the set alarm time, and the alarm rings. Output value: Visual representation of the alarm clock interface displays the alarm ringing with options to snooze or dismiss. Input values: 4. Snooze alarm - User selects the Snooze option to delay the alarm for a specified duration. Output value: Visual representation of the alarm clock interface displays the alarm snoozed for the specified duration. Input values: 5. Dismiss alarm - User selects the dismiss option to stop the alarm ringing. Output value: Visual representation of the alarm clock interface displays the alarm dismissed, and the interface returns to the regular state.
Solution 1: Basic Alarm Clock with Tkinter GUI
Prerequisites
- Install playsound for playing sound
- pip install playsound
- Install pygame if you haven’t already:
- pip install pygame
Code:
import tkinter as tk
from datetime import datetime
from time import sleep
from threading import Thread
import pygame
# Initialize pygame mixer
pygame.mixer.init()
# Function to check the current time and play the alarm sound
def check_alarm(set_time):
while True:
current_time = datetime.now().strftime("%H:%M")
if current_time == set_time:
pygame.mixer.music.load('morning.wav') # Load the sound file
pygame.mixer.music.play() # Play the sound
break
sleep(1)
# Function to set the alarm
def set_alarm():
alarm_time = f"{hour.get()}:{minute.get()}"
Thread(target=check_alarm, args=(alarm_time,)).start()
# Initialize main window
root = tk.Tk()
root.title("Alarm Clock")
root.geometry("300x200")
# Create labels and input fields for hour and minute
tk.Label(root, text="Set Alarm Time (24-hour format)").pack(pady=10)
hour = tk.Entry(root, width=5)
hour.pack(pady=5)
hour.insert(0, "HH")
minute = tk.Entry(root, width=5)
minute.pack(pady=5)
minute.insert(0, "MM")
# Button to set the alarm
set_button = tk.Button(root, text="Set Alarm", command=set_alarm)
set_button.pack(pady=20)
root.mainloop()
Output:
Explanation:
- Tkinter GUI: Uses Tkinter for creating a simple GUI with time input fields and a button to set the alarm.
- Alarm Sound: Uses the playsound library to play an alarm sound when the set time is reached.
- Multi-threading: Runs the alarm check in a separate thread to avoid freezing the GUI.
- User Input: User inputs the time in a 24-hour format.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/projects/python/python-project-create-a-basic-gui-alarm-clock.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics