python开发日历客户端软件项目练习

131 阅读2分钟

今天我们继续来练习python 编程,今天给大家分享的是用python做一个日历客户端,来巩固一下自己的python 语法,先给大家看一下 最后的成品效果

image.png

做出来的样子是不是还可以,嘿嘿。
源码部分代码如下:

    def refresh_calendar(self):
        """根据当前年份和月份刷新日期显示"""
        self.clear_calendar()  # 先清空旧控件

        # 更新标题显示格式:如 "2025年 6月"
        self.label_title.setText(f"{self.current_year}{self.current_month}月")

        # 计算当月第一天是星期几,Python中weekday()是0-周一,6-周日
        first_day = datetime.date(self.current_year, self.current_month, 1)
        start_weekday = first_day.weekday()  # 0-6,对应周一到周日

        # 计算当月天数,方法是获取下个月第一天与本月第一天的日期差
        if self.current_month == 12:
            next_month_first = datetime.date(self.current_year + 1, 1, 1)
        else:
            next_month_first = datetime.date(self.current_year, self.current_month + 1, 1)
        days_in_month = (next_month_first - first_day).days

        day_counter = 1  # 记录当月日期,1号开始

        # 网格最大6行7列(42格),保证完整显示整个月和前后空白
        for i in range(6):  # 行
            for j in range(7):  # 列
                cell_index = i * 7 + j  # 当前格子序号,从0开始
                # 判断是否达到显示当月第1天的起始格子,且日期未超出当月天数
                if cell_index >= start_weekday and day_counter <= days_in_month:
                    date_obj = datetime.date(self.current_year, self.current_month, day_counter)
                    is_today = (date_obj == self.today)  # 是否高亮今天
                    # 创建单个日期控件,传入日期和是否是今天
                    date_widget = DateWidget(date_obj, is_today)
                    self.grid_layout.addWidget(date_widget, i, j)
                    day_counter += 1
                else:
                    # 非当月日期格子用空白控件填充,保持网格整齐
                    spacer = QWidget()
                    spacer.setFixedSize(120, 80)  # 大小与日期控件相同
                    self.grid_layout.addWidget(spacer, i, j)

    # 以下为各种导航按钮对应的槽函数,切换年月后刷新日期显示

    def goto_prev_year(self):
        self.current_year -= 1
        self.refresh_calendar()

    def goto_next_year(self):
        self.current_year += 1
        self.refresh_calendar()

    def goto_prev_month(self):
        # 如果是1月,切换到去年12月
        if self.current_month == 1:
            self.current_month = 12
            self.current_year -= 1
        else:
            self.current_month -= 1
        self.refresh_calendar()

    def goto_next_month(self):
        # 如果是12月,切换到下一年1月
        if self.current_month == 12:
            self.current_month = 1
            self.current_year += 1
        else:
            self.current_month += 1
        self.refresh_calendar()

    def goto_today(self):
        """重置为今天的年月"""
        self.current_year = self.today.year
        self.current_month = self.today.month
        self.refresh_calendar()


if __name__ == '__main__':
    # PyQt应用启动入口
    app = QApplication(sys.argv)
    w = CalendarWidget()  # 创建主日历控件
    w.show()              # 显示窗口
    sys.exit(app.exec())  # 进入主事件循环

为了更好的熟悉python语法,基本代码旁边都写了备注信息

image.png

项目使用的两个库:
PyQt5:用于构建图形化界面(GUI),提供现代风格的按钮、标签、布局等组件。

lunar-python:轻量级农历计算库,实现从阳历自动转换到农历,并支持节气信息提取。
简单的操作说明:

python环境:3.9 以上

运行代码 安装两个 第三方库

PyQt5

lunar_python

安装命令 :

pip install PyQt5 lunar_python

打包exe 依赖

pip install pyinstaller

打包命令

pyinstaller --onefile --windowed main.py

有兴趣的小伙伴,可以拿去看看,希望能在你编程学习的过程中帮助到你。

完整代码和简单的操作说明已经打包好了。可以获取:

www.wwwoop.com/home/Index/…