In this Python tutorial, we will learn how to create a data time picker using Python Tkinter. You must have observed a calendar view appearance every time you click on the date option in a form. We will be creating a similar interface.
Are you new to Python tkinter, check out Python GUI Programming.
Create Environment in Python Tkinter
It is always a good practice to create an environment before installing any library. Creating an environment then installing the libraries allows you to share the project without the user worrying about the dependencies.
- We will be using the virtualenv package to create a virtual environment.
- We will install virtualenv globally. To install virtualenv globally simple type the below command:
pip install virtualenv
Possible error messages
- Requirement already satisfied if you are seeing this message that means virtualenv is already installed on your system.
- Command ‘pip’ not found this means that pip is not installed on your Linux or Mac machine. Click here to know the installation process.
- ‘pip’ is not recognized as an internal or external command this means that pip is not installed on your Windows machine. Click here to know how to install pip. If you are sure that pip is installed then it might not have been set to PATH. Click here to know how to add pip to the path.
Virtual environment on Windows
- Before creating a virtual environment create a project folder and move to that folder.
- In our case, we have created a project folder with the name date_picker, and now using the cmd we have moved to this folder using the command
cd date_picker
virtualenv env
- This command will create a virtual environment folder with the name env
- env is a standard name but you can keep whatever you like.
- Once the environment is created we need to activate the environment
- to activate the environment follow the below code.
env\Scripts\activate
virtual environment on Mac or Linux
- Before creating a virtual environment create a project folder and move to that folder.
- In our case, we have created a project folder with the name date_picker, and now using the terminal we have moved to this folder using the command
cd date_picker
virtualenv env
- This command will create a virtual environment folder with the name env
- env is a standard name but you can keep whatever you like.
- Once the environment is created we need to activate the environment
- to activate the environment follow the below code.
Source env/bin/activate
Read: Python Tkinter OptionMenu
Install Python Tkcalendar Library
Now, let us see how to install Python Tkcalendar library.
Now once, we have created and activated our virtual environment we need to install Python tkcalender to get started with the project.
tkcalendar is a python module that provides the Calendar and DateEntry widgets for Tkinter. The DateEntry widget is similar to a Combobox, but the drop-down is not a list but a Calendar to select a date.
Below is the code to install tkcalendar on your environment.
pip install tkcalendar
Creating Date Time Picker Using Python Tkinter
In this application, the user will be given a calendar to select a date & digital clock wherein the user can set up any time.
code:
from tkinter import *
from tkcalendar import *
ws = Tk()
ws.title("Python Guides")
ws.geometry("500x400")
ws.config(bg="#cd950c")
hour_string=StringVar()
min_string=StringVar()
last_value_sec = ""
last_value = ""
f = ('Times', 20)
def display_msg():
date = cal.get_date()
m = min_sb.get()
h = sec_hour.get()
s = sec.get()
t = f"Your appointment is booked for {date} at {m}:{h}:{s}."
msg_display.config(text=t)
if last_value == "59" and min_string.get() == "0":
hour_string.set(int(hour_string.get())+1 if hour_string.get() !="23" else 0)
last_value = min_string.get()
if last_value_sec == "59" and sec_hour.get() == "0":
min_string.set(int(min_string.get())+1 if min_string.get() !="59" else 0)
if last_value == "59":
hour_string.set(int(hour_string.get())+1 if hour_string.get() !="23" else 0)
last_value_sec = sec_hour.get()
fone = Frame(ws)
ftwo = Frame(ws)
fone.pack(pady=10)
ftwo.pack(pady=10)
cal = Calendar(
fone,
selectmode="day",
year=2021,
month=2,
day=3
)
cal.pack()
min_sb = Spinbox(
ftwo,
from_=0,
to=23,
wrap=True,
textvariable=hour_string,
width=2,
state="readonly",
font=f,
justify=CENTER
)
sec_hour = Spinbox(
ftwo,
from_=0,
to=59,
wrap=True,
textvariable=min_string,
font=f,
width=2,
justify=CENTER
)
sec = Spinbox(
ftwo,
from_=0,
to=59,
wrap=True,
textvariable=sec_hour,
width=2,
font=f,
justify=CENTER
)
min_sb.pack(side=LEFT, fill=X, expand=True)
sec_hour.pack(side=LEFT, fill=X, expand=True)
sec.pack(side=LEFT, fill=X, expand=True)
msg = Label(
ws,
text="Hour Minute Seconds",
font=("Times", 12),
bg="#cd950c"
)
msg.pack(side=TOP)
actionBtn =Button(
ws,
text="Book Appointment",
padx=10,
pady=10,
command=display_msg
)
actionBtn.pack(pady=10)
msg_display = Label(
ws,
text="",
bg="#cd950c"
)
msg_display.pack(pady=10)
ws.mainloop()
Output:
In this output, the user can book an appointment by selecting the date and time. Users can choose any date for the future or the past.
Scope for improvement
- add a restriction on the selection of past dates.
- Connect the application with the database and provide appointment confirmation only if the date & time is not occupied.
You may like the following Python tutorials:
- Python Pygame tutorial
- Machine Learning using Python
- How to Take User Input and Store in Variable using Python Tkinter
- Python Tkinter Exit Program
- How to attach an image in Turtle Python
- Python Tkinter TreeView Example
- Python Turtle Commands (15 useful commands)
- Upload a File in Python Tkinter
In this tutorial, we learned how to create a date time picker using Python tkinter.
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.