[Python] Restrict date selection from calendar to specific dates

Discussão em 'Python' iniciado por Stack, Agosto 6, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to let the user pick one out of a list of dates from my calendar but prevent her from selecting any other date.

    Based on this answer I managed to restrict the date range that can be selected but I couldn't exclude days in between.

    import datetime as dt
    import tkinter as tk
    from tkinter import ttk
    from tkcalendar import DateEntry

    # fmt: off
    dates = [
    "2024-04-08", "2024-04-10", "2024-04-11", "2024-04-12",
    "2024-04-15", "2024-04-16", "2024-04-17", "2024-04-18", "2024-04-19",
    "2024-04-22",
    "2024-05-21", "2024-05-22", "2024-05-23", "2024-05-24",
    "2024-05-27", "2024-05-28", "2024-05-29", "2024-05-30", "2024-05-31",
    "2024-06-03", "2024-06-04", "2024-06-05", "2024-06-07",
    "2024-06-10", "2024-06-11", "2024-06-12", "2024-06-13", "2024-06-14",
    ]
    # fmt: on

    root = tk.Tk()

    date_entry_var = tk.StringVar()
    date_entry = DateEntry(
    root,
    textvariable=date_entry_var,
    date_pattern="yyyy-mm-dd",
    mindate=dt.date.fromisoformat(dates[0]),
    maxdate=dt.date.fromisoformat(dates[-1]),
    )
    date_entry.pack()

    root.mainloop()


    Is there a way that makes it possible to select from a list of dates with a calendar widget?

    tk is required because I'm working with tk in the entire app but answer do not need to use the tkcalendar module if there are alternatives.

    Continue reading...

Compartilhe esta Página