Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Create a Date Picker Calendar in Tkinter
Tkinter is a popular Python library for creating desktop applications. It provides various widgets and methods to build interactive GUI applications with ease.
Tkcalendar is a specialized tkinter package that provides calendar widgets for GUI applications. It allows users to select dates, schedule events, and perform various calendar operations through an intuitive interface.
In this article, we will create a Date Picker calendar using the Tkcalendar package. First, install the package using pip install tkcalendar in your terminal or command prompt.
Basic Date Picker Example
Let's create a simple date picker with a calendar widget and a button to retrieve the selected date ?
# Import the libraries
from tkinter import *
from tkcalendar import *
# Create an instance of tkinter frame or window
win = Tk()
win.title("Date Picker Calendar")
win.geometry("400x400")
# Create calendar widget
cal = Calendar(win, selectmode="day", year=2024, month=1, day=1)
cal.pack(pady=20)
# Define function to get selected date
def get_date():
selected_date = cal.get_date()
label.config(text=f"Selected Date: {selected_date}")
# Create button to get the selected date
button = Button(win, text="Get Selected Date", command=get_date, bg="lightblue")
button.pack(pady=10)
# Create label to display selected date
label = Label(win, text="No date selected", font=("Arial", 12))
label.pack(pady=20)
win.mainloop()
Enhanced Date Picker with Features
Here's an improved version with additional features like date validation and formatting ?
from tkinter import *
from tkcalendar import Calendar, DateEntry
from datetime import date
# Create main window
root = Tk()
root.title("Advanced Date Picker")
root.geometry("500x450")
root.configure(bg="white")
# Title label
title_label = Label(root, text="Date Picker Calendar",
font=("Arial", 16, "bold"), bg="white")
title_label.pack(pady=10)
# Calendar widget with custom styling
cal = Calendar(root, selectmode='day',
year=2024, month=1, day=1,
background="darkblue",
foreground="white",
selectbackground="red")
cal.pack(pady=20)
# Function to display selected date
def show_selected_date():
selected = cal.get_date()
result_label.config(text=f"You selected: {selected}")
# Calculate days from today
today = date.today()
selected_date = cal.selection_get()
diff = (selected_date - today).days
if diff == 0:
day_info.config(text="That's today!")
elif diff > 0:
day_info.config(text=f"{diff} days from today")
else:
day_info.config(text=f"{abs(diff)} days ago")
# Buttons frame
button_frame = Frame(root, bg="white")
button_frame.pack(pady=10)
# Get date button
get_button = Button(button_frame, text="Get Selected Date",
command=show_selected_date,
bg="green", fg="white", font=("Arial", 10, "bold"))
get_button.pack(side=LEFT, padx=5)
# Clear button
def clear_selection():
result_label.config(text="No date selected")
day_info.config(text="")
clear_button = Button(button_frame, text="Clear",
command=clear_selection,
bg="red", fg="white", font=("Arial", 10, "bold"))
clear_button.pack(side=LEFT, padx=5)
# Result labels
result_label = Label(root, text="No date selected",
font=("Arial", 12), bg="white", fg="blue")
result_label.pack(pady=10)
day_info = Label(root, text="", font=("Arial", 10), bg="white", fg="gray")
day_info.pack()
root.mainloop()
Calendar Widget Parameters
The Calendar widget accepts several parameters for customization:
| Parameter | Description | Default |
|---|---|---|
selectmode |
Selection mode: "day" or "none" | "day" |
year |
Initial year displayed | Current year |
month |
Initial month (1-12) | Current month |
day |
Initial day selected | Current day |
background |
Calendar background color | System default |
Key Methods
Important methods for working with the Calendar widget:
-
get_date()- Returns selected date as string -
selection_get()- Returns selected date as date object -
selection_set(date)- Sets the selected date programmatically
Conclusion
Tkcalendar provides an easy way to add date picker functionality to tkinter applications. Use the Calendar widget for visual date selection and combine it with buttons and labels for a complete user interface.
