Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีแทรกเวลาปัจจุบันใน Entry Widget ใน Tkinter


เพื่อทำงานกับโมดูลวันที่และเวลา Python มีแพ็กเกจ 'datetime' ใช้ 'DateTime' แสดงวันที่ จัดการ datetime object และเขียนฟังก์ชันเพิ่มเติมในแอปพลิเคชัน

เพื่อแสดงวันที่ปัจจุบันในหน้าต่าง Tkinter ต้อง import โมดูล datetime ก่อน สร้างอินสแตนซ์ของ object แล้วแสดงด้วย Entry widget

ตัวอย่าง

ตัวอย่างแสดงวันที่ปัจจุบันใน 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 label widget 
entry=Entry(win,width=25, font=("Calibri", 25)) 
entry.insert(END,format_date) 
entry.pack() 
win.mainloop()

ผลลัพธ์

รันโค้ด จะแสดงวันที่ปัจจุบันใน Entry widget

วิธีแทรกเวลาปัจจุบันใน Entry Widget ใน Tkinter