如何在 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.