Guest User

Untitled

a guest
Jan 24th, 2026
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import tkinter as tk
  2. from datetime import datetime
  3. import yaml
  4.  
  5. def load_tasks(filename):
  6. with open(filename, 'r') as file:
  7. tasks = file.readlines()
  8. return [task.strip() for task in tasks]
  9.  
  10. def save_diary(diary, filename):
  11. today = datetime.now().strftime('%Y-%m-%d')
  12.  
  13. try:
  14. with open(filename, 'r+') as file:
  15. data = yaml.safe_load(file) or {}
  16.  
  17. if today not in data:
  18. data[today] = {}
  19.  
  20. # Move the file pointer to the beginning and write the updated data
  21. file.seek(0)
  22. yaml.dump(data, file)
  23. file.truncate()
  24. except FileNotFoundError:
  25. with open(filename, 'w') as file:
  26. yaml.dump({today: {}}, file)
  27.  
  28. def log_click(task, diary):
  29. today = datetime.now().strftime('%Y-%m-%d')
  30. current_time = datetime.now().strftime('%Y-%m-%d %H:%M')
  31.  
  32. if today not in diary:
  33. diary[today] = {}
  34.  
  35. if task not in diary[today]:
  36. diary[today][task] = []
  37.  
  38. diary[today][task].append(current_time)
  39. save_diary(diary, 'diary.yaml')
  40.  
  41. def load_today_data(filename):
  42. today = datetime.now().strftime('%Y-%m-%d')
  43.  
  44. try:
  45. with open(filename, 'r') as file:
  46. data = yaml.safe_load(file) or {}
  47. return data.get(today, {})
  48. except FileNotFoundError:
  49. return {}
  50.  
  51. def update_buttons():
  52. for widget in frame.winfo_children():
  53. widget.destroy()
  54.  
  55. tasks.sort()
  56. today_clicked = [task for task in tasks if task in diary.get(datetime.now().strftime('%Y-%m-%d'), {})]
  57. other_tasks = [task for task in tasks if task not in today_clicked]
  58.  
  59. all_tasks = sorted(other_tasks) + sorted(today_clicked)
  60.  
  61. for task in all_tasks:
  62. button = tk.Button(frame, text=f"{task} ({diary.get(datetime.now().strftime('%Y-%m-%d'), {}).get(task, 0)})")
  63. button.config(command=lambda t=task: (log_click(t, diary), update_buttons()))
  64. button.pack(fill=tk.X)
  65.  
  66. if __name__ == '__main__':
  67. tasks = load_tasks('tasks.txt')
  68. diary = load_today_data('diary.yaml')
  69.  
  70. root = tk.Tk()
  71. root.title("Daily Routines Diary")
  72.  
  73. frame = tk.Frame(root)
  74. frame.pack(fill=tk.BOTH, expand=True)
  75.  
  76. update_buttons()
  77.  
  78. root.mainloop()
  79.  
Advertisement
Add Comment
Please, Sign In to add comment