How to insert the current time in an Entry Widget in Tkinter?


To work with the date and time module, Python provides the 'datetime' package. Using the 'DateTime' package, we can display the date, manipulate the datetime object and use it to write the additional functionality in the application.

To display the current date in a Tkinter window, we've to first import the datetime module in our environment. Once imported, you can create an instance of its object and display it using the Entry widget.

Example

Here is an example of how you can show the current date in an Entry widget.

# Import the required libraries
from tkinter import *
import datetime as dt

# Create an instance of tkinter frame or window
win=Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Create an instance of datetime module
date=dt.datetime.now()

# Format the date
format_date=f"{date:%a, %b %d %Y}"

# Display the date in a a label widget
entry=Entry(win,width=25, font=("Calibri", 25))
entry.insert(END,format_date)
entry.pack()

win.mainloop()

Output

Running the above code will display the current date in an Entry widget.

Updated on: 06-Aug-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements