python实现桌面万年历

桌面万年历

功能如下:

1 万年历
2 农历
3 年月日时分秒
4 天气
5 历史上的今天
6 每日一句

软件桌面效果如下:


桌面万年历
代码如下:
import tkinter as tk
from tkinter import ttk
import calendar
import requests
from datetime import datetime
# import datetime

# 天气API地址,需要替换为实际有效的API
WEATHER_API = "https://restapi.amap.com/v3/weather/weatherInfo?city=440105&key=538e4b9ec070d1bbc2cf265399dde4d5"
# 节假日API地址,需要替换为实际有效的API 20250401


# HOLIDAY_API = "https://www.mxnzp.com/api/holiday/single/20181121?ignoreHoliday=false&app_id=lqj9q1ivvnrtx0qo&app_secret=OuNT0aQxK7nhGbacUklUQCbV4rYFClrn"

# 获取当前系统的年月日
now = datetime.now()
year = now.year
month = now.month
day = now.day
# 将年、月、日格式化为 YYYYMMDD 格式
formatted_date = f"{year:04d}{month:02d}{day:02d}"
# print(f"当前系统日期: {year}-{month}-{day}")
# print(f"格式化后的日期: {formatted_date}")

# 凭借字符串,产生今天的农历日期
HOLIDAY_API = r"https://www.mxnzp.com/api/holiday/single/" + formatted_date + r"?ignoreHoliday=false&app_id=lqj9q1ivvnrtx0qo&app_secret=OuNT0aQxK7nhGbacUklUQCbV4rYFClrn"

# 历史上的今天API地址,需要替换为实际有效的API
HISTORY_API = "https://www.mxnzp.com/api/history/today?type=1&app_id=lqj9q1ivvnrtx0qo&app_secret=OuNT0aQxK7nhGbacUklUQCbV4rYFClrn"
# 每日语录API地址,需要替换为实际有效的API
QUOTE_API = "https://www.mxnzp.com/api/daily_word/recommend?count=10&app_id=lqj9q1ivvnrtx0qo&app_secret=OuNT0aQxK7nhGbacUklUQCbV4rYFClrn"

class WeatherApp:
    def __init__(self, root):
        self.root = root
        self.root.title("综合信息展示")

        # 创建日历
        self.calendar_frame = ttk.Frame(root)
        self.calendar_frame.pack(pady=10)
        self.cal = calendar.TextCalendar(calendar.SUNDAY)
        year = datetime.now().year
        month = datetime.now().month
        cal_text = self.cal.formatmonth(year, month)
        self.calendar_label = ttk.Label(self.calendar_frame, text=cal_text, font=("Courier New", 12))
        self.calendar_label.pack()

        # 获取并显示天气信息
        self.weather_frame = ttk.Frame(root)
        self.weather_frame.pack(pady=10)
        weather_data = self.get_weather()
        # 修改此处以正确提取信息
        if weather_data.get("status") == "1" and weather_data.get("lives"):
            live_info = weather_data["lives"][0]
            weather_text = f"地区: 广州海珠\n天气: {live_info.get('weather', '未知')}\n温度: {live_info.get('temperature', '未知')}°C\n湿度: {live_info.get('humidity', '未知')}%\n风速: {live_info.get('windpower', '未知')}级"
        else:
            weather_text = "天气信息获取失败"

        self.weather_label = ttk.Label(self.weather_frame, text=weather_text, font=("Arial", 12))
        self.weather_label.pack()

        # 获取并显示今日信息
        self.today_frame = ttk.Frame(root)
        self.today_frame.pack(pady=10)
        def update_time():
            nonlocal today_text
            todaytext = f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
            self.today_label.config(text=todaytext)  # 修改这里:从today_frame改为today_label
            self.root.after(1000, update_time)  # 每秒更新一次

        today_text = f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
        self.today_label = ttk.Label(self.today_frame, text=today_text, font=("Arial", 18))
        self.today_label.pack()
        update_time()

        # 获取并显示节假日信息
        self.holiday_frame = ttk.Frame(root)
        self.holiday_frame.pack(pady=10)
        holiday_data = self.get_holiday()["data"]
        holiday_text = f"农历日: {holiday_data.get('yearTips', '无')}年{holiday_data.get('lunarCalendar', '无')}\n\n     星期{holiday_data.get('weekDay', '无')}\n\n     {holiday_data.get('typeDes', '无')}"
        self.holiday_label = ttk.Label(self.holiday_frame, text=holiday_text, font=("Arial", 12))
        self.holiday_label.pack()

        # 获取并显示历史上的今天信息
        self.history_frame = ttk.Frame(root)
        self.history_frame.pack(pady=10)
        history_data = self.get_history()["data"][0]

        history_text = f"历史上的今天: \n{self.wrap_text(history_data.get('title', '无'))}"
        self.history_label = ttk.Label(self.history_frame, text=history_text, font=("Arial", 12))
        self.history_label.pack()

        # 获取并显示每日语录信息
        self.quote_frame = ttk.Frame(root)
        self.quote_frame.pack(pady=10)
        quote_data = self.get_quote()["data"][0]
        quote_text = f"每日语录: \n{self.wrap_text(quote_data.get('content', '无'))}"
        self.quote_label = ttk.Label(self.quote_frame, text=quote_text, font=("Arial", 12))
        self.quote_label.pack()


    def wrap_text(self,text, max_length=15):
        lines = []
        for i in range(0, len(text), max_length):
            lines.append(text[i:i + max_length])
        return '\n'.join(lines)

    def get_weather(self):
        try:
            response = requests.get(WEATHER_API)
            return response.json()
        except Exception as e:
            print(f"获取天气信息失败: {e}")
            return {}

    def get_holiday(self):
        try:
            response = requests.get(HOLIDAY_API)
            return response.json()
        except Exception as e:
            print(f"获取节假日信息失败: {e}")
            return {}

    def get_history(self):
        try:
            response = requests.get(HISTORY_API)
            return response.json()
        except Exception as e:
            print(f"获取历史上的今天信息失败: {e}")
            return {}

    def get_quote(self):
        try:
            response = requests.get(QUOTE_API)
            return response.json()
        except Exception as e:
            print(f"获取每日语录信息失败: {e}")
            return {}

if __name__ == "__main__":
    root = tk.Tk()
    app = WeatherApp(root)
    root.mainloop()

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容