Home » Tutorials » How to Make a Calendar using Python

How to Make a Calendar using Python

Imagine having the power to organize your days, weeks, and months with a custom calendar, all built from scratch by you. Yes, that’s exactly what I’m going to do in this tutorial. I’ll walk you through creating your very own calendar using Python, where users can select dates, add events to those selected dates, and also make a list where they can view those selected dates.

Let’s get started!

Table of Contents

Example

This is the Final result that you will get after following this tutorial:

Necessary Libraries

Let’s get everything set up before we jump into the code part. So, make sure to install these libraries using the terminal or your command prompt for the code to function properly:

$ pip install tk
$ pip install tkcalendar 

Imports

from tkinter import *
from tkcalendar import Calendar
from tkinter import messagebox

We start by importing the tkinter library which allows us to create a graphical user interface, and then the Calendar module together with the messagebox module from tkinter library so we can display dialog message boxes.

Functions

Now, we’re diving into the heart of our project, where the real magic happens with our crucial functions:

Show Selected Dates Function

def show_selected_dates(calendar, event_text, selected_dates, events):
   event_text.delete(1.0, END)

   for selected_date in selected_dates:
       events_for_date = events.get(selected_date, [])
       event_text.insert(END, f"Events for {selected_date}:\n")
       for event in events_for_date:
           event_text.insert(END, f"- {event}\n")

First, we create this function that updates the events displayed in the main window.

Add Event Function

def add_event(calendar, event_text, selected_dates, events, event_entry):
   event = event_entry.get()


   if not event:
       messagebox.showwarning("Empty Event", "Please enter an event.")
       return


   for selected_date in selected_dates:
       if selected_date not in events:
           events[selected_date] = []
       events[selected_date].append(event)


   # Update the displayed events
   show_selected_dates(calendar, event_text, selected_dates, events)


   # Clear the event entry
   event_entry.delete(0, END)

Then we associate an event to a selected date and display them on the main window by storing them in the events Dictionary and triggering the previous function to update them.

Select Date Function

def select_date(calendar, selected_dates):
   selected_date = calendar.get_date()
   selected_dates.append(selected_date)
   messagebox.showinfo("Selected Date", f"You selected: {selected_date}")

This one adds the selected date to the list of selected dates.

Create Calendar Function

def create_calendar(root):
   # Create a calendar with a specific date pattern
   calendar = Calendar(root, setmode="day", date_pattern='d/m/yy')
   calendar.pack(padx=15, pady=15)
   return calendar

Next, we create a calendar widget that allows the user to select individual days which is suitable for date selection.

Create a Select Date Button Function

def create_select_date_button(root, calendar, event_text, selected_dates, events):
   # Create a button to select the date
   select_date_button = Button(root, text="Select Date", command=lambda: select_date(calendar, selected_dates))
   select_date_button.pack(padx=15, pady=5)


   # Create an entry for adding events
   event_entry_label = Label(root, text="Add Event:")
   event_entry_label.pack(pady=5)


   event_entry = Entry(root, width=20)
   event_entry.pack(pady=5)


   # Create a button to add events
   add_event_button = Button(root, text="Add Event",
                             command=lambda: add_event(calendar, event_text, selected_dates, events, event_entry))
   add_event_button.pack(pady=15)

The function presented here creates labels and an entry widget where the user inputs the event as well as the two buttons “Select Date” and “Add Event” with each of them triggering the select_date and add_event function.

Create Event Text Function

def create_event_text(root):
   # Create a Text widget to display events
   event_text = Text(root, height=8, width=30)
   event_text.pack(padx=15, pady=15)
   return event_text

Here, we created a text widget that displays events and gives it a specified height and width.

Main Function

def main():
   root = Tk()
   root.title("Improved Calendar App - The Pycodes ")
   root.geometry("400x530")
   root.configure(bg="lightblue")

   # Lists to store selected dates
   selected_dates = []

   # Dictionary to store events for each date
   events = {}

   calendar = create_calendar(root)
   event_text = create_event_text(root)
   create_select_date_button(root, calendar, event_text, selected_dates, events)

   root.mainloop()

This main function sets up the Main Window giving it a title, and a background color defining its geometry as well as initializing the storage of the dates and events, and setting up also the calendar and event text widget together with the “date selection” and “event addition” buttons. More importantly, it allows the user to interact with the graphical user interface.

Program Entry Point

if __name__ == "__main__":
   main()

Lastly, this part ensures that the main function is executed when the script is run directly.

Full Code

from tkinter import *
from tkcalendar import Calendar
from tkinter import messagebox



def show_selected_dates(calendar, event_text, selected_dates, events):
   event_text.delete(1.0, END)


   for selected_date in selected_dates:
       events_for_date = events.get(selected_date, [])
       event_text.insert(END, f"Events for {selected_date}:\n")
       for event in events_for_date:
           event_text.insert(END, f"- {event}\n")



def add_event(calendar, event_text, selected_dates, events, event_entry):
   event = event_entry.get()


   if not event:
       messagebox.showwarning("Empty Event", "Please enter an event.")
       return


   for selected_date in selected_dates:
       if selected_date not in events:
           events[selected_date] = []
       events[selected_date].append(event)


   # Update the displayed events
   show_selected_dates(calendar, event_text, selected_dates, events)


   # Clear the event entry
   event_entry.delete(0, END)



def select_date(calendar, selected_dates):
   selected_date = calendar.get_date()
   selected_dates.append(selected_date)
   messagebox.showinfo("Selected Date", f"You selected: {selected_date}")



def create_calendar(root):
   # Create a calendar with a specific date pattern
   calendar = Calendar(root, setmode="day", date_pattern='d/m/yy')
   calendar.pack(padx=15, pady=15)
   return calendar



def create_select_date_button(root, calendar, event_text, selected_dates, events):
   # Create a button to select the date
   select_date_button = Button(root, text="Select Date", command=lambda: select_date(calendar, selected_dates))
   select_date_button.pack(padx=15, pady=5)


   # Create an entry for adding events
   event_entry_label = Label(root, text="Add Event:")
   event_entry_label.pack(pady=5)


   event_entry = Entry(root, width=20)
   event_entry.pack(pady=5)


   # Create a button to add events
   add_event_button = Button(root, text="Add Event",
                             command=lambda: add_event(calendar, event_text, selected_dates, events, event_entry))
   add_event_button.pack(pady=15)



def create_event_text(root):
   # Create a Text widget to display events
   event_text = Text(root, height=8, width=30)
   event_text.pack(padx=15, pady=15)
   return event_text



def main():
   root = Tk()
   root.title("Improved Calendar App - The Pycodes ")
   root.geometry("400x530")
   root.configure(bg="lightblue")


   # Lists to store selected dates
   selected_dates = []


   # Dictionary to store events for each date
   events = {}


   calendar = create_calendar(root)
   event_text = create_event_text(root)
   create_select_date_button(root, calendar, event_text, selected_dates, events)


   root.mainloop()


if __name__ == "__main__":
   main()

Happy Coding!

2 thoughts on “How to Make a Calendar using Python”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top