Skip to content

Commit ddf42c2

Browse files
authored
Update main.py
Add buttons which add/remove allowed dates.
1 parent 90472d4 commit ddf42c2

File tree

1 file changed

+59
-1
lines changed
  • tkinter/tkcalendar - add allowed_dates

1 file changed

+59
-1
lines changed

tkinter/tkcalendar - add allowed_dates/main.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
dates = [
1717
"2024-04-08", "2024-04-10", "2024-04-11", "2024-04-12",
1818
"2024-04-15", "2024-04-16", "2024-04-17", "2024-04-18", "2024-04-19",
19-
"2024-04-22",
19+
"2024-04-22",
20+
2021
"2024-05-21", "2024-05-22", "2024-05-23", "2024-05-24",
2122
"2024-05-27", "2024-05-28", "2024-05-29", "2024-05-30", "2024-05-31",
23+
2224
"2024-06-03", "2024-06-04", "2024-06-05", "2024-06-07",
2325
"2024-06-10", "2024-06-11", "2024-06-12", "2024-06-13", "2024-06-14",
2426
]
@@ -53,4 +55,60 @@
5355
)
5456
date_entry.pack()
5557

58+
def show_allowed_dates():
59+
for date in cal['allowed_dates']: # not `cal.allowed_dates`
60+
print('allowed:', date)
61+
62+
button_show = tk.Button(root, text="Show Allowed Dates", command=show_allowed_dates)
63+
button_show.pack(fill='x')
64+
65+
def add_allowed_date(date):
66+
dt_date = dt.date.fromisoformat(date)
67+
68+
if dt_date not in cal['allowed_dates']:
69+
print('add allowed:', date)
70+
71+
cal['allowed_dates'].append(dt_date)
72+
# other methods
73+
#cal['allowed_dates'] += [append(dt.date.fromisoformat(date)]
74+
#cal['allowed_dates'].extend( [append(dt.date.fromisoformat(date)] )
75+
76+
cal['allowed_dates'] = sorted(cal['allowed_dates'])
77+
78+
# what if new date is not in `mindate`, `maxdate` ???
79+
if cal['allowed_dates'][0] < cal['mindate']:
80+
cal['mindate'] = cal['allowed_dates'][0]
81+
82+
if cal['allowed_dates'][-1] > cal['maxdate']:
83+
cal['maxdate'] = cal['allowed_dates'][-1]
84+
85+
# redraw it
86+
cal._display_calendar()
87+
88+
for date in ('2024-06-06', '2024-06-26', '2024-07-10'):
89+
button_add = tk.Button(root, text=f"Add Allowed Date: {date}", command=lambda x=date:add_allowed_date(x))
90+
button_add.pack(fill='x')
91+
92+
def remove_allowed_date(date):
93+
dt_date = dt.date.fromisoformat(date)
94+
95+
if dt_date in cal['allowed_dates']:
96+
print('remove allowed:', date)
97+
98+
cal['allowed_dates'].remove(dt_date)
99+
100+
# what if removed date is `mindate` or `maxdate` ???
101+
if cal['mindate'] < cal['allowed_dates'][0]:
102+
cal['mindate'] = cal['allowed_dates'][0]
103+
104+
if cal['maxdate'] > cal['allowed_dates'][-1]:
105+
cal['maxdate'] = cal['allowed_dates'][-1]
106+
107+
# redraw it
108+
cal._display_calendar()
109+
110+
for date in ('2024-06-06', '2024-06-26', '2024-07-10'):
111+
button_remove = tk.Button(root, text=f"Remove Allowed Date: {date}", command=lambda x=date:remove_allowed_date(x))
112+
button_remove.pack(fill='x')
113+
56114
root.mainloop()

0 commit comments

Comments
 (0)