使用 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()

輸出

執行以上程式碼將建立一個日曆,我們可以在其中選擇一個特定日期。

更新於:2021-03-04

2 千+ 瀏覽次數

開啟你的 職業

透過完成課程以獲得認證

開始學習
廣告