Tkinter เป็นไลบรารี Python ยอดนิยมสำหรับสร้างและพัฒนาแอปพลิเคชัน มีเมธอดและฟังก์ชันต่างๆ สำหรับเพิ่มฟีเจอร์หลายอย่าง
Tkcalendar เป็นหนึ่งในแพ็กเกจ Tkinter ที่ใช้สร้าง Calendar แบบ GUI ในหน้าต่าง ดังนั้นเราสามารถทำหลาย Operation เช่น เลือกวันที่ Pick และ Schedule Event ผ่านแอปพลิเคชัน Calendar และอื่นๆ
อย่างไรก็ตาม ในบทความนี้ เราจะดูวิธีสร้าง Date Picker Calendar โดยใช้แพ็กเกจ Tkcalendar ก่อนอื่น ต้องติดตั้งแพ็กเกจใน Local Environment โดยใช้ 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()
ผลลัพธ์
การรันโค้ดข้างต้นจะสร้าง Calendar ที่เราสามารถเลือกวันที่เฉพาะได้
