To create a calendar with the ability to add to-do items in Python, you can use the calendar
, tkinter
, and datetime
modules. Here's an example code that demonstrates how to create a basic calendar application with a reminder functionality:
import calendar
from datetime import datetime
import tkinter as tk
def add_to_do():
day = int(selected_date.get())
month = int(current_month.get())
year = int(current_year.get())
date = datetime(year, month, day)
todo_item = new_todo.get()
if date not in todo_items:
todo_items[date] = [todo_item]
else:
todo_items[date].append(todo_item)
new_todo.delete(0, tk.END)
def show_reminders():
reminders.delete("1.0", tk.END)
day = int(selected_date.get())
month = int(current_month.get())
year = int(current_year.get())
date = datetime(year, month, day)
if date in todo_items:
for item in todo_items[date]:
reminders.insert(tk.END, f"- {item}\n")
today = datetime.now()
todo_items = {}
# Create the tkinter GUI
root = tk.Tk()
root.title("Calendar with To-Do List")
# Create the calendar widget
cal = calendar.Calendar(firstweekday=calendar.SUNDAY)
days = cal.itermonthdays4(today.year, today.month)
current_month = tk.StringVar(value=str(today.month))
current_year = tk.StringVar(value=str(today.year))
selected_date = tk.StringVar()
calendar_frame = tk.Frame(root)
calendar_frame.pack()
for day, weekday, month, year in days:
if month != today.month:
btn_color = "grey"
else:
btn_color = "white"
if day == 0:
# This day is outside the month boundary
continue
btn = tk.Button(calendar_frame, text=str(day), width=3, bg=btn_color, command=lambda day=day: selected_date.set(day))
btn.grid(row=weekday, column=month, padx=5, pady=5)
# Create the to-do list widgets
todo_frame = tk.Frame(root)
todo_frame.pack()
new_todo = tk.Entry(todo_frame, width=25)
new_todo.grid(row=0, column=0, padx=5, pady=5)
add_btn = tk.Button(todo_frame, text="Add to-do", command=add_to_do)
add_btn.grid(row=0, column=1, padx=5, pady=5)
reminders = tk.Text(todo_frame, width=25, height=10)
reminders.grid(row=1, column=0, columnspan=2)
show_btn = tk.Button(todo_frame, text="Show reminders", command=show_reminders)
show_btn.grid(row=2, column=0, columnspan=2, pady=5)
# Create the month and year selection widgets
month_selector = tk.OptionMenu(root, current_month, *range(1, 13))
month_selector.pack(side=tk.LEFT, padx=5, pady=5)
year_selector = tk.OptionMenu(root, current_year, *range(today.year-10, today.year+11))
year_selector.pack(side=tk.LEFT, padx=5, pady=5)
# Add a label to display the selected date
date_label = tk.Label(root, textvariable=selected_date)
date_label.pack(padx=5, pady=5)
root.mainloop()
This code will create a basic GUI that displays a calendar for the current month and year, along with a text box for adding to-do items and a button to display reminders for the selected date. To add a to-do item, simply enter the item and click the "Add to-do" button. The item will be associated with the selected date on the calendar. To display reminders for a specific date, click the "Show reminders" button after selecting a date on the calendar.
You can customize this code to fit your specific needs, such as adding the ability to save or load to-do items from a file, or to send reminders to a notification service.