เพื่อทำงานกับโมดูลวันที่และเวลา Python มีแพ็กเกจ 'datetime' ใช้ 'datetime' แสดงวันที่ จัดการ datetime object และเขียนฟังก์ชันเพิ่มเติม
แสดงวันที่ปัจจุบันในหน้าต่าง Tkinter: import โมดูล datetime สร้างอินสแตนซ์ object แล้วแสดงด้วย label widget
ตัวอย่าง
ตัวอย่างแสดงวันที่ปัจจุบันในหน้าต่าง Tkinter
# 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 label widget
label=Label(win, text=format_date, font=("Calibri", 25))
label.pack()
win.mainloop()
ผลลัพธ์
รันโค้ด จะแสดงวันที่ปัจจุบันในหน้าต่าง
