How to print the calendar of any year in Python using Tkinter

To create a Graphical User Interface (GUI) calendar, where users can input their year of choice, press enter, and obtain a calendar for each month of the year, we can make use of multiple GUI packages. One of the easiest to use is the ‘tkinter’ package available in Python. The packages used in this tutorial are:

  • Python ‘tkinter‘ package
  • Python ‘calendar‘ package

Let’s understand how to use Tkinter and Calendar to create a GUI calendar.

How to print the calendar of a specific year in Python using Tkinter

We know that ‘tkinter’ provides a GUI, but what about ‘calendar’? This package provides useful calendar data such as the names of days and months in both full and abbreviated forms, and the number of days in a month. Therefore, the first step is to import both the packages required for this tutorial.

from tkinter import *
import calendar
We have now imported all classes, functions, and variables from the ‘tkinter’ module as well as the ‘calendar’ package. To build the interactive GUI, there are two more steps:
  • write a function to display a calendar for a selected year
  • write a ‘main’ program that initializes the GUI, sets up the event loop, and starts the application

Write a function to display a calendar for a selected year

We will now define a function to display the calendar for any user input year. Within the function definition, we first create a new ‘Tkinter’ window, set up the window’s geometry (display size), title, and background color.

def display_calendar():
    calendar_window = Tk()
    calendar_window.geometry("1000x1000")
    calendar_window.title("Calendar for the Selected Year")
    calendar_window.config(background = 'lightblue')

Once this is done, we move on to processing and creating the calendar for the user input year. This is done in the following sequential steps:

  • obtaining the input year and converting it from ‘string’ to ‘int’ type 
  • generating the calendar content for the selected year
  • creating a ‘Label’ widget to display the calendar content
  • placing the label within a grid layout, and
  • starting the event loop for the calendar window
from tkinter import *
import calendar

def display_calendar():
    calendar_window = Tk()
    calendar_window.geometry("1000x1000")
    calendar_window.title("Calendar for the Selected Year")
    calendar_window.config(background = 'lightblue')
    
    selected_year = int(year_entry.get())
    calendar_content = calendar.calendar(selected_year)
    calendar_label = Label(calendar_window, text = calendar_content, font = ("Courier", 20), justify = LEFT)
    calendar_label.grid(row = 5, column = 1)

    calendar_window.mainloop()

While creating the ‘Label’ widget, it is important to note that the font is set to monospace font “Courier” and the calendar content is LEFT justified to ensure correct arrangement of each year’s calendar data.

This completes the calendar window function to display any year’s calendar. We can now define the ‘main’ function for our project. Let’s understand how to do so.

Write a ‘main’ program that initializes the GUI, sets up the event loop, and starts the application

The ‘main’ function is created to serve as the user’s entry point to our GUI application. To build this function, we first have to create a Main window and define its properties including window size, title, and  background color.
if __name__ == '__main__':
    main_window = Tk()
    main_window.geometry("450x500")
    main_window.title("Year Calendar Application")
    main_window.config(background = 'lightblue')

We can now get to creating the display of the Main window: labels, a user entry field, and buttons (to display and to exit).

if __name__ == '__main__':
    main_window = Tk()
    main_window.geometry("450x500")
    main_window.title("Year Calendar Application")
    main_window.config(background = 'lightblue')
    
    title_label = Label(main_window, text = "Year Calendar Application", font = ("Times", 35, "bold",))
    year_label = Label(main_window, text = "Enter Year: ", bg = 'orange', font = ("Times", 25, "bold"))
    year_entry = Entry(main_window, font = ("Times", 20, "bold"))
    show_button = Button(main_window, text = "Display Calendar", command = display_calendar, font = ("Times", 20, "bold"))
    exit_button = Button(main_window, text = "Exit", command = main_window.destroy, font = ("Times", 20, "bold"))

The last two steps are to specify the grid layout and start the event loop for the ‘main’ function. Here, we use the same approach that was previously used for the ‘display_calendar()’ function.

if __name__ == '__main__':
    main_window = Tk()
    main_window.geometry("450x500")
    main_window.title("Year Calendar Application")
    main_window.config(background = 'lightblue')
    
    
    title_label = Label(main_window, text = "Year Calendar Application", font = ("Times", 35, "bold",))
    year_label = Label(main_window, text = "Enter Year: ", bg = 'orange', font = ("Times", 25, "bold"))
    year_entry = Entry(main_window, font = ("Times", 20, "bold"))
    show_button = Button(main_window, text = "Display Calendar", command = display_calendar, font = ("Times", 20, "bold"))
    exit_button = Button(main_window, text = "Exit", command = main_window.destroy, font = ("Times", 20, "bold"))
    
    title_label.grid(row = 1, column = 1)
    year_label.grid(row = 2, column = 1)
    year_entry.grid(row = 3, column = 1)
    show_button.grid(row = 4, column = 1)
    exit_button.grid(row = 5, column = 1)
    
    main_window.mainloop()

Now that both the functions are defined, we can put them together to create a completed program. The full code is below for reference:

from tkinter import *
import calendar

def display_calendar():
    calendar_window = Tk()
    calendar_window.geometry("1000x1000")
    calendar_window.title("Calendar for the Selected Year")
    calendar_window.config(background = 'lightblue')
    
    selected_year = int(year_entry.get())
    calendar_content = calendar.calendar(selected_year)
    calendar_label = Label(calendar_window, text = calendar_content, font = ("Courier", 20), justify = LEFT)
    calendar_label.grid(row = 5, column = 1)

    calendar_window.mainloop()

if __name__ == '__main__':
    main_window = Tk()
    main_window.geometry("450x500")
    main_window.title("Year Calendar Application")
    main_window.config(background = 'lightblue')
    
    
    title_label = Label(main_window, text = "Year Calendar Application", font = ("Times", 35, "bold",))
    year_label = Label(main_window, text = "Enter Year: ", bg = 'orange', font = ("Times", 25, "bold"))
    year_entry = Entry(main_window, font = ("Times", 20, "bold"))
    show_button = Button(main_window, text = "Display Calendar", command = display_calendar, font = ("Times", 20, "bold"))
    exit_button = Button(main_window, text = "Exit", command = main_window.destroy, font = ("Times", 20, "bold"))
    
    title_label.grid(row = 1, column = 1)
    year_label.grid(row = 2, column = 1)
    year_entry.grid(row = 3, column = 1)
    show_button.grid(row = 4, column = 1)
    exit_button.grid(row = 5, column = 1)

    main_window.mainloop()

The above code uses Python packages to create a GUI that can be used to generate a calendar of any user input year. Check it out and don’t forget to run it yourself!

I hope you enjoyed this tutorial. Thank you for reading.

Leave a Comment

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

Scroll to Top