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

จะแทรกเวลาปัจจุบันใน Entry Widget ใน Tkinter ได้อย่างไร?


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

ในการแสดงวันที่ปัจจุบันในหน้าต่าง Tkinter ก่อนอื่นเราต้องนำเข้าโมดูล datetime ในสภาพแวดล้อมของเรา เมื่อนำเข้าแล้ว คุณสามารถสร้างอินสแตนซ์ของวัตถุและแสดงโดยใช้วิดเจ็ตรายการ

ตัวอย่าง

นี่คือตัวอย่างวิธีการแสดงวันที่ปัจจุบันในวิดเจ็ตรายการ

# 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
entry=Entry(win,width=25, font=("Calibri", 25))
entry.insert(END,format_date)
entry.pack()

win.mainloop()

ผลลัพธ์

การเรียกใช้โค้ดด้านบนจะแสดงวันที่ปัจจุบันในวิดเจ็ตรายการ

จะแทรกเวลาปัจจุบันใน Entry Widget ใน Tkinter ได้อย่างไร?