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