使用 Tkinter 创建日期选择器日历
Tkinter 是一个流行的 Python 库,用于创建和开发应用程序。它拥有各种可用于在应用程序中添加多项功能的方法和函数。
Tkcalendar 是可用于在窗口中创建基于 GUI 的日历的 Tkinter 程序包之一,因此,我们可以通过日历应用程序执行多种操作,如选择数据、挑选和安排事件等等。
不过,在本文中,我们将看到如何使用 Tkcalendar 程序包创建日期选择器日历。在此之前,我们必须使用 pip install tkcalendar 将该程序包安装到本地环境中。
安装后,我们将创建 Tkcalendar 实例并创建一个按钮以获取日期。
示例
#Import the libraries
from tkinter import *
from tkcalendar import *
#Create an instance of tkinter frame or window
win= Tk()
win.title("Calendar")
win.geometry("700x600")
cal= Calendar(win, selectmode="day",year= 2021, month=3, day=3)
cal.pack(pady=20)
#Define Function to select the date
def get_date():
label.config(text=cal.get_date())
#Create a button to pick the date from the calendar
button= Button(win, text= "Select the Date", command= get_date)
button.pack(pady=20)
#Create Label for displaying selected Date
label= Label(win, text="")
label.pack(pady=20)
win.mainloop()输出
运行以上代码将创建一个日历,我们可以在其中选择一个特定日期。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP