Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Birthday Reminder Application ใน Python

สร้างแอปพลิเคชันแจ้งเตือนวันเกิด: ตรวจสอบทุกวันว่ามีวันเกิดวันนี้หรือไม่ แจ้งเตือนผ่าน System Notification


โครงสร้าง Lookup File

ไฟล์ birth_day_lookup.txt (เก็บที่ Home Directory)

05-September John Doe
12-January Jane Smith
25-December Bob Wilson

รูปแบบ: DD-MonthName FirstName LastName


โค้ดหลัก

import os
import time

file_path = os.path.join(os.getenv('HOME'), 'birth_day_lookup.txt')

def check_birthday():
    with open(file_path, 'r') as f:
        today = time.strftime('%d-%B')  # 05-September
        bday_flag = 0
        
        for entry in f:
            if today in entry:
                parts = entry.strip().split(' ')
                name = ' '.join(parts[1:])  # FirstName LastName
                os.system(f'notify-send "Today is {name}\'s Birthday"')
                bday_flag = 1
        
        if bday_flag == 0:
            os.system('notify-send "No birthday for today is listed"')

if __name__ == "__main__":
    check_birthday()

ตั้งค่าให้รันอัตโนมัติตอน Startup (Linux)

ขั้นตอนที่ 1: ทำให้ Executable

chmod +x birthday_reminder.py

ขั้นตอนที่ 2: ย้ายไป /usr/bin

sudo cp birthday_reminder.py /usr/bin/birthday_reminder

ขั้นตอนที่ 3: เพิ่มใน Startup Applications

  1. เปิด "Startup Applications"
  2. กด Add
  3. Name: Birthday Reminder
  4. Command: birthday_reminder
  5. Save

ปรับปรุงเพิ่มเติม (Enhancements)

  • ใช้ plyer หรือ dbus แทน notify-send เพื่อ Cross-platform
  • รองรับปีกะลัง (Leap Year) สำหรับ 29 กุมภาพันธ์
  • เพิ่ม GUI จัดการ Birthday List
  • ส่ง Email/SMS นอกจาก Desktop Notification
  • ใช้ Cron Job แทน Startup App (รันทุกเช้า 06:00)
# Cron Example (รันทุกวัน 06:00)
# crontab -e
0 6 * * * /usr/bin/python3 /path/to/birthday_reminder.py