清明时节雨纷纷,路上行人欲断魂。大家伙都踏上了寻找太公的漫长道路,猴子也不例外,猴子今天也找了一早上了。中午吃完饭回来洗了个澡,然后开始了漫长的一个下午。无聊的一天,就干一些无聊的事情吧。逢年过节,高铁票、火车票都是一票难求,不是抢不到就是忘记了购票。今年好不容易抢到了清明的,却忘记了还要返程.............................................就趁着现在无聊的时间,随手写了一个放假倒计时,由于高铁站提前15天放票,猴子还贴心的添加了抢票提醒。需要注意的是,抢票提醒,只是提醒,不是帮你抢票哈......猴子不才,用的是网上大佬们的农历算法,模块库的库名叫:lunarcalendar,部分核心代码如下:def update_holiday_list(self): """更新节日列表,显示今年剩余节日 + 明年重要节日(元旦、春节)""" for item in self.tree.get_children(): self.tree.delete(item) today = datetime.date.today() current_year = today.year next_year = current_year + 1 holidays = self.holiday_data.get_all_holidays(current_year) upcoming_holidays = [(name, date) for name, date in holidays if date > today] next_year_holidays = self.holiday_data.get_all_holidays(next_year) next_year_upcoming = [ (name, date) for name, date in next_year_holidays if name in ["元旦", "春节"] ] all_holidays = upcoming_holidays + next_year_upcoming all_holidays.sort(key=lambda x: x[1]) for name, date in all_holidays: days_left = (date - today).days weekday = self.get_chinese_weekday(date) date_str = f"{date.strftime('%Y-%m-%d')} ({weekday})" display_name = name if date.year == next_year: display_name = f"明年{name}" festival_name = self.get_festival_with_date(display_name, date) self.tree.insert('', tk.END, values=(festival_name, date_str, days_left)) self.tree.tag_configure('oddrow', background='#f0f0f0') self.tree.tag_configure('evenrow', background='#ffffff') for i, item in enumerate(self.tree.get_children()): self.tree.item(item, tags=('evenrow' if i % 2 == 0 else 'oddrow',))def get_next_holiday(self): """获取下一个节日及其倒计时""" today = datetime.date.today() current_year = today.year next_year = current_year + 1 holidays = self.get_all_holidays(current_year) + self.get_all_holidays(next_year) print(holidays) upcoming_holidays = [(name, date) for name, date in holidays if date >= today] upcoming_holidays.sort(key=lambda x: x[1]) if not upcoming_holidays: return "没有找到节日", None, 0 name, date = upcoming_holidays[0] days_left = (date - today).days return name, date, days_left
由于设置了提前15天的高铁购票提醒,时间还未到,所以这里不展示,只展示代码。def update_display(self):"""更新界面显示"""today = datetime.date.today()self.current_date_value.config(text=today.strftime("%Y年%m月%d日"))name, date, days = self.holiday_data.get_next_holiday()if days == 0:self.next_holiday_label.config(text="今天是: ", style='Holiday.TLabel')self.next_holiday_name.config(text=f"{name}!", foreground='#e74c3c')if name == "清明节":self.next_holiday_date.config(text="清明时节,缅怀先人")self.countdown_value.config(text="🕯️")elif name == "中元节":self.next_holiday_date.config(text="中元祭祖,慎终追远")self.countdown_value.config(text="🙏")elif name in ["春节", "元旦", "劳动节", "国庆节", "端午节", "中秋节", "元宵节"]:self.next_holiday_date.config(text="节日快乐!")self.countdown_value.config(text="🎉")elif name == "重阳节":self.next_holiday_date.config(text="敬老爱老,登高祈福")self.countdown_value.config(text="👵👴")else:self.next_holiday_date.config(text="")self.countdown_value.config(text="📅")else:self.next_holiday_label.config(text="下一个节日: ", style='Holiday.TLabel')if days <= 15:self.next_holiday_name.config(text=f"{name}即将到来", foreground='#e67e22')self.next_holiday_date.config(text=date.strftime("%Y年%m月%d日"))if name == "清明节":self.countdown_value.config(text=f"{days}天n(提前安排行程)",foreground='#3498db',font=('微软雅黑', 18, 'bold'))else:self.countdown_value.config(text=f"{days}天n(提前购票!)",foreground='#e74c3c',font=('微软雅黑', 18, 'bold'))try:if name == "清明节":icon_url = "https://img.icons8.com/color/48/000000/cemetery.png"else:icon_url = "https://img.icons8.com/ios/50/train.png"response = requests.get(icon_url, timeout=3)holiday_icon = ImageTk.PhotoImage(Image.open(BytesIO(response.content)))if hasattr(self, 'train_icon_label'):self.train_icon_label.config(image=holiday_icon)self.train_icon_label.image = holiday_iconelse:self.train_icon_label = ttk.Label(self.next_holiday_frame, image=holiday_icon)self.train_icon_label.grid(row=0, column=3, rowspan=3, padx=10)self.train_icon_label.image = holiday_iconexcept:passif not hasattr(self, 'ticket_tip_label'):tip_text = {"清明节": "清明节期间出行人多,请提前安排行程","中元节": "中元节祭祖,请提前规划行程",}.get(name, "高铁票通常在节前15天开售,请及时购票回家过节!")self.ticket_tip_label = ttk.Label(self.next_holiday_frame,text=tip_text,foreground='#e74c3c',font=('微软雅黑', 9, 'italic'))self.ticket_tip_label.grid(row=3, column=0, columnspan=3, pady=5, sticky=tk.W)else:self.next_holiday_name.config(text=name, foreground='#2980b9')self.next_holiday_date.config(text=date.strftime("%Y年%m月%d日"))self.countdown_value.config(text=f"{days} 天")if hasattr(self, 'train_icon_label'):self.train_icon_label.grid_forget()if hasattr(self, 'ticket_tip_label'):self.ticket_tip_label.grid_forget()self.update_holiday_list()
还没有评论,来说两句吧...