Certainly! Below is a complete Python program using only built-in libraries (tkinter, calendar, and datetime) to create a simple calendar GUI. Each day is a text box where the user can enter notes, and the notes can be saved and loaded for each month/year. The user can navigate months and years.
How it works:
- The calendar is displayed as a grid of days for the selected month/year.
- Each day is a
Text widget for user input.
- Notes are saved to a file named
calendar_data.json in the same directory.
- Navigation buttons allow changing the month and year.
import tkinter as tk
from tkinter import ttk
import calendar
import datetime
import json
import os
DATA_FILE = "calendar_data.json"
class CalendarApp:
def __init__(self, root):
self.root = root
self.root.title("Python Calendar")
self.today = datetime.date.today()
self.current_year = self.today.year
self.current_month = self.today.month
self.data = self.load_data()
self.header_frame = tk.Frame(self.root)
self.header_frame.pack(pady=10)
self.prev_btn = tk.Button(self.header_frame, text="<", width=3, command=self.prev_month)
self.prev_btn.grid(row=0, column=0)
self.month_year_var = tk.StringVar()
self.month_year_label = tk.Label(self.header_frame, textvariable=self.month_year_var, font=("Arial", 16))
self.month_year_label.grid(row=0, column=1, padx=10)
self.next_btn = tk.Button(self.header_frame, text=">", width=3, command=self.next_month)
self.next_btn.grid(row=0, column=2)
self.year_spin = tk.Spinbox(self.header_frame, from_=1900, to=2100, width=5, command=self.change_year)
self.year_spin.grid(row=0, column=3, padx=10)
self.year_spin.delete(0, tk.END)
self.year_spin.insert(0, self.current_year)
self.calendar_frame = tk.Frame(self.root)
self.calendar_frame.pack()
self.save_btn = tk.Button(self.root, text="Save Notes", command=self.save_notes)
self.save_btn.pack(pady=5)
self.day_boxes = {} # (day): Text widget
self.draw_calendar()
def load_data(self):
if os.path.exists(DATA_FILE):
with open(DATA_FILE, "r") as f:
return json.load(f)
return {}
def save_data(self):
with open(DATA_FILE, "w") as f:
json.dump(self.data, f)
def get_notes(self, year, month, day):
return self.data.get(str(year), {}).get(str(month), {}).get(str(day), "")
def set_notes(self, year, month, day, notes):
self.data.setdefault(str(year), {}).setdefault(str(month), {})[str(day)] = notes
def draw_calendar(self):
for widget in self.calendar_frame.winfo_children():
widget.destroy()
self.day_boxes.clear()
self.month_year_var.set(f"{calendar.month_name[self.current_month]} {self.current_year}")
# Weekday headers
for i, day in enumerate(calendar.day_abbr):
lbl = tk.Label(self.calendar_frame, text=day, font=("Arial", 10, "bold"), borderwidth=1, relief="solid", width=12, height=2)
lbl.grid(row=0, column=i, sticky="nsew")
cal = calendar.Calendar()
month_days = cal.monthdayscalendar(self.current_year, self.current_month)
for week_num, week in enumerate(month_days):
for day_num, day in enumerate(week):
frame = tk.Frame(self.calendar_frame, borderwidth=1, relief="solid")
frame.grid(row=week_num+1, column=day_num, sticky="nsew")
if day == 0:
# Empty day
lbl = tk.Label(frame, text="", width=12, height=5)
lbl.pack()
else:
day_label = tk.Label(frame, text=str(day), anchor="nw")
day_label.pack(anchor="nw")
text = tk.Text(frame, width=12, height=3, wrap="word")
text.pack()
notes = self.get_notes(self.current_year, self.current_month, day)
text.insert("1.0", notes)
self.day_boxes[day] = text
def save_notes(self):
for day, text_widget in self.day_boxes.items():
notes = text_widget.get("1.0", tk.END).strip()
self.set_notes(self.current_year, self.current_month, day, notes)
self.save_data()
tk.messagebox.showinfo("Saved", "Notes saved successfully!")
def prev_month(self):
if self.current_month == 1:
self.current_month = 12
self.current_year -= 1
else:
self.current_month -= 1
self.year_spin.delete(0, tk.END)
self.year_spin.insert(0, self.current_year)
self.draw_calendar()
def next_month(self):
if self.current_month == 12:
self.current_month = 1
self.current_year += 1
else:
self.current_month += 1
self.year_spin.delete(0, tk.END)
self.year_spin.insert(0, self.current_year)
self.draw_calendar()
def change_year(self):
try:
year = int(self.year_spin.get())
if 1900 <= year <= 2100:
self.current_year = year
self.draw_calendar()
except ValueError:
pass
if __name__ == "__main__":
root = tk.Tk()
app = CalendarApp(root)
root.mainloop()
How to use:
- Run the script.
- The current month is displayed. Each day is a box where you can type notes.
- Use
< and > to change months, or the spinbox to change the year.
- Click "Save Notes" to save your notes for the current month/year.
- Notes are automatically loaded when you revisit a month/year.
No external libraries are required.