Python生日提醒應用


在本節中,我們將學習如何使用Python建立一個生日提醒應用程式。

問題陳述

建立一個使用Python的應用程式,該應用程式可以檢查當天是否有生日。如果當天是名單上某個人的生日,則向系統傳送包含該人姓名的通知。

我們需要一個檔案,我們可以將日期、月份和人員姓名儲存在這個檔案中,作為該應用程式的查詢檔案。該檔案如下所示:

Problem Statement

我們將把這個應用程式轉換為一個啟動應用程式,以便在系統啟動時啟動。

建立生日提醒應用程式的步驟

  • 獲取查詢檔案並從中讀取。
  • 檢查日期和月份是否與當前日期和月份匹配。
    • 向系統傳送通知,通知所有今天過生日的人的姓名。
  • 停止

示例程式碼

importos, time
#Take the birthday lookup file from home directory
file_path = os.getenv('HOME') + '/birth_day_lookup.txt'
defcheck_birthday():
   lookup_file = open(file_path, 'r') #open the lookup file as read mode
   today = time.strftime('%d-%B') #get the todays date as dd-Month format
   bday_flag = 0
   #loop through each entry in the birthday file, and check whether the day is present or not
   for entry inlookup_file:
      if today in entry:
         line = entry.split(' ') #cut the line on spaces to get name and surname
         bday_flag = 1
         os.system('notify-send "Today is '+line[1]+' '+line[2]+''s Birthday"')
   ifbday_flag == 0:
      os.system('notify-send "No birthday for today is listed"')
check_birthday()

輸出

Output

將生日提醒設定為啟動應用程式的步驟

步驟1 - 使用chmod命令將指令碼檔案轉換為可執行檔案。

sudochmod +x file_name.py

步驟2 - 將指令碼檔案移動到/usr/bin目錄。

sudocp file_name.py /usr/bin

步驟3 - 現在搜尋啟動應用程式,並啟動它。

Startup Application

開啟應用程式後,轉到新增,輸入所需名稱,然後在命令欄位中輸入程式名稱。並將其新增為啟動應用程式。

Command Field

更新於:2019年7月30日

680 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始
廣告