Computer >> Computer tutorials >  >> Programming >> Python

How to get the current date to display in a Tkinter window?


To work with the date and time module, Python provides the 'datetime' package. Using 'datetime' package, we can display the date, manipulate the datetime object, and use it to write the additional functionality in an 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 them using the label widget.

Example

Here is an example of how you can show the current date in a Tkinter window.

# 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
label=Label(win, text=format_date, font=("Calibri", 25))
label.pack()

win.mainloop()

Output

Running the above code will display the current date in the window.

How to get the current date to display in a Tkinter window?