|
| 1 | +#!/usr/bin/env python3.13 |
| 2 | + |
| 3 | +# date: 2025.08.06 |
| 4 | + |
| 5 | +# [python - Restrict date selection from calendar to specific dates - Stack Overflow](https://stackoverflow.com/questions/79727284/restrict-date-selection-from-calendar-to-specific-dates) |
| 6 | + |
| 7 | +import datetime as dt |
| 8 | +import tkinter as tk |
| 9 | +from tkinter import ttk |
| 10 | +#from tkcalendar import DateEntry |
| 11 | + |
| 12 | +from mycalendar import Calendar |
| 13 | +from mycalendar import DateEntry |
| 14 | + |
| 15 | +# fmt: off |
| 16 | +dates = [ |
| 17 | + "2024-04-08", "2024-04-10", "2024-04-11", "2024-04-12", |
| 18 | + "2024-04-15", "2024-04-16", "2024-04-17", "2024-04-18", "2024-04-19", |
| 19 | + "2024-04-22", |
| 20 | + "2024-05-21", "2024-05-22", "2024-05-23", "2024-05-24", |
| 21 | + "2024-05-27", "2024-05-28", "2024-05-29", "2024-05-30", "2024-05-31", |
| 22 | + "2024-06-03", "2024-06-04", "2024-06-05", "2024-06-07", |
| 23 | + "2024-06-10", "2024-06-11", "2024-06-12", "2024-06-13", "2024-06-14", |
1 | 24 | ] |
2 | 25 | # fmt: on |
3 | 26 |
|
4 | 27 | root = tk.Tk() |
5 | 28 |
|
6 | 29 | dt_dates = [ dt.date.fromisoformat(date) for date in dates ] |
7 | 30 |
|
8 | | -cal = mycalendar.Calendar( |
| 31 | +# example mycalendar.Calendar |
| 32 | +cal = Calendar( |
9 | 33 | root, |
10 | 34 | date_pattern="yyyy-mm-dd", |
11 | | - mindate=dt.date.fromisoformat(dates[0]), |
12 | | - maxdate=dt.date.fromisoformat(dates[-1]), |
| 35 | + mindate=dt_dates[0], |
| 36 | + maxdate=dt_dates[-1], |
13 | 37 | allowed_dates=dt_dates, |
14 | | - locale="en_GB.utf-8", # to show it in English instead of my native Polish |
| 38 | + #locale="en_GB.utf-8", # to show it in English instead of my native Polish |
| 39 | + # to make screenshot |
15 | 40 | ) |
16 | 41 | cal.pack() |
17 | 42 |
|
18 | | -#date_entry_var = tk.StringVar() |
19 | | -#date_entry = DateEntry( |
20 | | -# root, |
21 | | -# textvariable=date_entry_var, |
22 | | -# date_pattern="yyyy-mm-dd", |
23 | | -# mindate=dt.date.fromisoformat(dates[0]), |
24 | | -# maxdate=dt.date.fromisoformat(dates[-1]), |
25 | | -#) |
26 | | -#date_entry.pack() |
| 43 | +date_entry_var = tk.StringVar() |
| 44 | + |
| 45 | +# example mycalendar.DateEntry |
| 46 | +date_entry = DateEntry( |
| 47 | + root, |
| 48 | + textvariable=date_entry_var, |
| 49 | + date_pattern="yyyy-mm-dd", |
| 50 | + mindate=dt_dates[0], |
| 51 | + maxdate=dt_dates[-1], |
| 52 | + allowed_dates=dt_dates, |
| 53 | +) |
| 54 | +date_entry.pack() |
27 | 55 |
|
28 | 56 | root.mainloop() |
0 commit comments