Here’s a Python program that creates a simple GUI-based calendar using the tkinter library and Python’s built-in calendar module.
The program will display the calendar for a specified month and year.
Code
import tkinter as tk
import calendar
# Function to display the calendar for the given month and year
def show_calendar():
try:
# Get the year and month from the user inputs
year = int(year_entry.get())
month = int(month_entry.get())
# Generate the calendar text
cal_text = calendar.month(year, month)
# Update the calendar display label with the generated calendar
calendar_display.config(state="normal")
calendar_display.delete("1.0", tk.END) # Clear previous content
calendar_display.insert(tk.END, cal_text) # Insert the new calendar
calendar_display.config(state="disabled") # Make the text box read-only
except ValueError:
calendar_display.config(state="normal")
calendar_display.delete("1.0", tk.END)
calendar_display.insert(tk.END, "Please enter valid numbers for year and month.")
calendar_display.config(state="disabled")
# Initialize the main window
root = tk.Tk()
root.title("Calendar Viewer")
root.geometry("300x300")
# Labels and entry fields for year and month
tk.Label(root, text="Year:").pack(pady=5)
year_entry = tk.Entry(root)
year_entry.pack(pady=5)
tk.Label(root, text="Month:").pack(pady=5)
month_entry = tk.Entry(root)
month_entry.pack(pady=5)
# Button to display the calendar
show_button = tk.Button(root, text="Show Calendar", command=show_calendar)
show_button.pack(pady=10)
# Text widget to display the calendar
calendar_display = tk.Text(root, width=20, height=8, font=("Courier", 12))
calendar_display.pack(pady=10)
calendar_display.config(state="disabled") # Set to read-only
# Run the main application loop
root.mainloop()
Explanation
- Import Required Libraries:
- tkinter: Used to create the GUI components.
- calendar: Provides functions to generate text-based calendars.
- Define show_calendar Function:
- This function generates and displays the calendar for a specified month and year.
- Get User Input:
- year = int(year_entry.get()): Retrieves and converts the entered year from the year_entry field.
- month = int(month_entry.get()): Retrieves and converts the entered month from the month_entry field.
- Generate Calendar Text:
- cal_text = calendar.month(year, month): Generates a text-based calendar for the specified year and month using the calendar.month function, which returns a string containing the formatted calendar.
- Display Calendar in the Text Widget:
- calendar_display.config(state=”normal”): Sets the Text widget to be editable so we can update it.
- calendar_display.delete(“1.0”, tk.END): Clears any previous calendar from the display.
- calendar_display.insert(tk.END, cal_text): Inserts the generated calendar text into the Text widget.
- calendar_display.config(state=”disabled”): Sets the Text widget to read-only after updating to prevent user modification.
- Error Handling:
- If the user enters non-numeric values for year or month, a ValueError is raised, and an error message is displayed instead of the calendar.
- Initialize the Main Application Window:
- root = tk.Tk(): Creates the main application window.
- root.title(“Calendar Viewer”): Sets the title of the window to “Calendar Viewer”.
- root.geometry(“300×300”): Sets the window size to 300×300 pixels.
- Create Labels and Entry Fields for Year and Month:
- tk.Label(root, text=”Year:”) and year_entry = tk.Entry(root): A label and entry field for entering the year.
- tk.Label(root, text=”Month:”) and month_entry = tk.Entry(root): A label and entry field for entering the month.
- .pack(pady=5): Adds some vertical padding to space out the elements.
- Create “Show Calendar” Button:
- show_button = tk.Button(root, text=”Show Calendar”, command=show_calendar): A button labeled “Show Calendar” that calls show_calendar when clicked.
- Text Widget to Display the Calendar:
- calendar_display = tk.Text(…): Creates a Text widget to display the calendar output.
- width=20 and height=8: Set the size of the widget to display the calendar text without scrollbars.
- font=(“Courier”, 12): Sets the font to Courier to ensure the calendar text is displayed in a monospaced font, aligning columns properly.
- calendar_display.config(state=”disabled”): Initially sets the Text widget to read-only.
- Run the Main Event Loop:
- root.mainloop(): Starts the tkinter main event loop, which keeps the application window open and responsive to user actions.
Sample Usage
- Enter Year and Month:
- The user enters the desired year (e.g., 2024) and month (e.g., 11 for November).
- Show Calendar:
- Clicking “Show Calendar” generates and displays the calendar for November 2024.
- Display Calendar:
- The Text widget displays the formatted calendar for the selected month and year.
Sample Output
If the user enters Year: 2024 and Month: 11, the output in the Text widget might look like this:
November 2024
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
Notes
- Error Handling: If the user enters an invalid year or month, a message is displayed to prompt for correct input.
- Monospaced Font: The Courier font ensures that each character has the same width, aligning the calendar columns correctly.
Summary
This Python program provides a simple, GUI-based calendar viewer using tkinter.
It allows users to input a year and month and then displays the corresponding calendar using Python’s calendar module.
The program handles basic errors and ensures correct formatting for the calendar output.