How to Create a Date Time Picker using Python Tkinter?

As a developer working on various projects, I recently faced the need to add a user-friendly date and time selection feature to my GUI application. After researching various options, I found that combining the Tkinter with the tkcalendar and CustomTkinter libraries provides an elegant solution. In this tutorial, I will explain how to create a Date Time Picker using Python Tkinter.

Create a Date Time Picker using Python Tkinter

Let us learn how to create a date time picker using Python Tkinter.

Read How to Create a Filter() Function in Python Tkinter?

Step 1: Import Required Libraries

To begin, let’s import the necessary libraries in our Python script:

import tkinter as tk
from tkcalendar import DateEntry
import customtkinter as ctk

Step 2: Create the Tkinter Window

Next, we’ll create a Tkinter window to hold our Date Time Picker:

window = tk.Tk()
window.title("Date Time Picker")
window.geometry("400x300")

Here, we create a window with the title “Date Time Picker” and set its dimensions to 400×300 pixels.

Check out How to Create a Python Tkinter Panel with Search Functionality?

Step 3: Create the Date Picker

Now, let’s add a date picker to our window using the DateEntry widget from the tkcalendar library:

date_label = tk.Label(window, text="Select Date:")
date_label.pack()

date_picker = DateEntry(window, width=12, background='darkblue', foreground='white', borderwidth=2)
date_picker.pack(padx=10, pady=10)

We create a label to indicate the purpose of the date picker and then create the DateEntry widget itself. We customize its appearance by setting the width, background color, foreground color, and border width.

Read How to Create a Python Tkinter Panel with Search Functionality?

Step 4: Create the Time Picker

To add a time picker, we’ll use the Spinbox widget from Tkinter:

time_label = tk.Label(window, text="Select Time:")
time_label.pack()

hour_var = tk.StringVar(value=12)
minute_var = tk.StringVar(value=00)

time_frame = tk.Frame(window)
time_frame.pack()

hour_spinbox = tk.Spinbox(time_frame, from_=1, to=12, textvariable=hour_var, wrap=True, width=5)
hour_spinbox.pack(side=tk.LEFT)

tk.Label(time_frame, text=":").pack(side=tk.LEFT)

minute_spinbox = tk.Spinbox(time_frame, from_=00, to=59, textvariable=minute_var, wrap=True, width=5)
minute_spinbox.pack(side=tk.LEFT)

We create a label for the time picker and then define two StringVar variables to hold the hour and minute values. We create a frame to hold the hour and minute spinboxes and pack them side by side with a colon separator in between.

Check out How to Create Scrollable Frames with Python Tkinter?

Step 5: Create a Submit Button

Lastly, let’s add a submit button to retrieve the selected date and time:

def submit_clicked():
    selected_date = date_picker.get_date()
    selected_hour = hour_var.get()
    selected_minute = minute_var.get()
    print(f"Selected Date: {selected_date}")
    print(f"Selected Time: {selected_hour}:{selected_minute}")

submit_button = ctk.CTkButton(window, text="Submit", command=submit_clicked)
submit_button.pack(padx=10, pady=10)

We define a function called submit_clicked() that retrieves the selected date using the get_date() method of the DateEntry widget and the selected hour and minute using the get() method of the respective StringVar variables. We then print the selected values.

We create a CTkButton from the CustomTkinter library set its text to “Submit”, and assigned the submit_clicked function to its command attribute.

Read How to Display Images in Python Tkinter?

Step 6: Run the Tkinter Event Loop

Finally, we start the Tkinter event loop to display the window and handle user interactions:

window.mainloop()

And that’s it! We now have a functional Date Time Picker created using Python Tkinter.

You can look at the output in the screenshot below.

Create a Date Time Picker using Python Tkinter

Check out How to Use Colors in Python Tkinter?

Conclusion

In this tutorial, I helped you learn how to create a Date Time Picker using Python Tkinter. I discussed a step-by-step process to create date time picker and also added functionality to display the selected date and time on the terminal.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.