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


To display the current date in tkinter window, we will use the datetime library.

date = dt.datetime.now()

Steps −

  • Import the required libraries and create an instance of tkinter frame.

  • Set the size of the frame using geometry method.

  • Call datetime.now() and store the value in a variable "date".

  • Next, create a label to display the date. In the text parameter of the label, pass the date value and format the data as text=f"{date:%A, %B %d, %Y}".

    • %A – Day of the week, full name

    • %B – Full month name

    • %d – Day of the month

    • %Y – Year with century as a decimal number

  • Finally, run the mainloop of the application window.

Example −

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

# Create an instance of tkinter
win = Tk()
win.title("Display Current Date")
win.geometry("700x350")

date = dt.datetime.now()
# Create Label to display the Date
label = Label(win, text=f"{date:%A, %B %d, %Y}", font="Calibri, 20")
label.pack(pady=20)

win.mainloop()

Output

On execution, it will produce the following output −

Updated on: 26-Oct-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements