如何在 Tkinter 中的 Entry Widget 中插入當前時間?


為了使用日期和時間模組,Python 提供了“datetime”包。使用 **“DateTime”** 包,我們可以顯示日期,處理 datetime 物件,並使用它編寫應用程式中的附加功能。

要在 Tkinter 視窗中顯示當前日期,我們首先要在我們的環境中匯入 datetime 模組。匯入後,你可以建立一個其物件的例項,並使用 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 a label widget
entry=Entry(win,width=25, font=("Calibri", 25))
entry.insert(END,format_date)
entry.pack()

win.mainloop()

輸出

執行上述程式碼將在 Entry Widget 中顯示當前日期。

更新於: 2021 年 8 月 6 日

2K+ 瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.