专栏导读
-
🌸 欢迎来到Python办公自动化专栏—Python处理办公问题,解放您的双手
-
-
-
-
📕 此外还有python基础专栏:请点击——>Python基础学习专栏求订阅
-
文章作者技术和水平有限,如果文中出现错误,希望大家能指正🙏
-
❤️ 欢迎各位佬关注! ❤️
库的介绍
-
要在代码中实现检查日期是否为休息日(如周六、周日或法定节假日),
-
并相应地标注它们,可以使用 chinese_calendar 库。
-
这个库提供了中国节假日的查询功能并能帮助您判断任意日期是否为工作日、周末或节假日。
库的安装
pip install chinese_calenda -i https://pypi.tuna.tsinghua.edu.cn/simple/
完整代码
import tkinter as tk
from tkinter import ttk, scrolledtext
from tkcalendar import DateEntry
from datetime import timedelta
from chinese_calendar import is_holiday, is_workday, get_holiday_detail
def generate_dates():
start_date = start_cal.get_date()
end_date = end_cal.get_date()
if start_date and end_date:
delta = end_date - start_date
time_list = []
text_widget.config(state=tk.NORMAL)
text_widget.delete('1.0', tk.END)
for i in range(delta.days + 1):
date = start_date + timedelta(days=i)
date_str = date.strftime("%Y-%m-%d")
if not is_workday(date):
if date.weekday() == 5:
time_list.append(f"{date_str} 休息(周六)")
elif date.weekday() == 6:
time_list.append(f"{date_str} 休息(周日)")
else:
holiday_name = get_holiday_detail(date)
time_list.append(f"{date_str} 法定节假日:{holiday_name}")
else:
time_list.append(date_str)
for date_info in time_list:
text_widget.insert(tk.END, date_info + "\n")
text_widget.config(state=tk.DISABLED)
else:
text_widget.config(state=tk.NORMAL)
text_widget.delete('1.0', tk.END)
text_widget.insert(tk.END, "Please select both start and end dates.\n")
text_widget.config(state=tk.DISABLED)
root = tk.Tk()
root.title("Date Range Generator")
start_label = ttk.Label(root, text="Start Date:")
start_label.pack()
start_cal = DateEntry(root, width=12, background='darkgray', foreground='white', selectbackground='lightblue')
start_cal.pack()
end_label = ttk.Label(root, text="End Date:")
end_label.pack()
end_cal = DateEntry(root, width=12, background='darkgray', foreground='white', selectbackground='lightblue')
end_cal.pack()
generate_button = ttk.Button(root, text="Generate Dates", command=generate_dates)
generate_button.pack()
text_widget = scrolledtext.ScrolledText(root, state=tk.DISABLED, height=20, width=60)
text_widget.pack()
root.mainloop()
总结
-
希望对初学者有帮助
-
致力于办公自动化的小小程序员一枚
-
希望能得到大家的【一个免费关注】!感谢
-
求个 🤞 关注 🤞
-
-
求个 ❤️ 喜欢 ❤️
-
-
求个 👍 收藏 👍
-