用 Python 写一个“会下小纸条雨”的暖心程序 —— Flask 网页版 + Tkinter 桌面版

最近我写了一个小小的 Python 程序------它会在屏幕上不断弹出彩色小纸条,每一张都写着一句温暖的话:"多喝水哦~"、"你已经很棒了!"、"记得吃水果"......

这不是什么高深技术,而是一份用代码传递的心意

今天,我把这个项目整理成两个版本分享出来:

  • 网页版(Flask):部署在服务器上,打开浏览器就能看到满屏飘落的温暖。
  • 桌面版(Tkinter):双击运行,直接在 Windows/Linux 桌面弹出小窗口。

代码极简,只有两个文件,新手也能轻松上手!


一、网页版:Flask 实现"永不消失的小纸条雨"

📁 目录结构(仅两个目录!)

复制代码
warm_web/
├── app.py
└── templates/
    └── index.html

🔧 app.py(后端逻辑)

python 复制代码
# -*- coding: utf-8 -*-
from flask import Flask, render_template
import random

app = Flask(__name__)
TIPS = [
    '多喝水哦~',
    '保持微笑呀',
    '每天都要元气满满',
    '记得吃水果',
    '保持好心情',
    '好好爱自己',
    '梦想成真',
    '期待下一次见面',
    '恭喜发财',
    '顺顺利利',
    '早点休息',
    '愿所有烦恼都消失',
    '别熬夜',
    '今天过得开心嘛',
    '天冷了,多穿衣服',
    '你已经很棒了!',
    '深呼吸,一切都会好起来的',
    '阳光总在风雨后',
    '给自己一个拥抱吧',
    '慢慢来,不着急',
    '今天的你也很努力呢',
    '世界因你而温柔',
    '记得抬头看看天空',
    '吃顿好吃的犒劳自己',
    '有人正在默默关心你',
    '你的存在很有意义',
    '累了就休息一下吧',
    '明天会是崭新的一天',
    '心怀希望,继续前行',
    '你值得被爱和幸福'
]

@app.route('/')
def index():
    return render_template('index.html', name='夏雨', tips=TIPS)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=82, debug=True)

💖 templates/index.html(前端效果)

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>给 {{ name }} 的温暖</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            height: 100vh;
            overflow: hidden;
            background: #f0f9ff;
            font-family: "Microsoft YaHei", sans-serif;
        }
        .tip-box {
            position: absolute;
            width: 240px;
            padding: 12px;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            font-size: 16px;
            font-weight: bold;
            text-align: center;
            opacity: 0;
            animation: fadeIn 1.5s forwards;
            z-index: 1000;
            word-wrap: break-word;
            pointer-events: none; /* 防止遮挡点击 */
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to   { opacity: 1; transform: translateY(0); }
        }

        /* 清理按钮样式 */
        #clearBtn {
            position: fixed;
            bottom: 20px;
            right: 20px;
            padding: 8px 16px;
            background: #ff6b9d;
            color: white;
            border: none;
            border-radius: 20px;
            cursor: pointer;
            font-size: 14px;
            z-index: 9999;
        }
        #clearBtn:hover {
            background: #ff4d8a;
        }
    </style>
</head>
<body>
    <button id="clearBtn">清空所有</button>

    <script>
        const name = "{{ name }}";
        const tips = {{ tips | tojson }};
        const colors = [
            '#ffcce6', '#cce5ff', '#ccffcc', '#e6ccff',
            '#ffffcc', '#ffccff', '#ffcccc', '#ffe0cc',
            '#ccffff', '#ffe6e6', '#e6ffe6', '#f9e6ff', '#fff2e6'
        ];

        function createTip() {
            const tip = name + "," + tips[Math.floor(Math.random() * tips.length)];
            const color = colors[Math.floor(Math.random() * colors.length)];

            const div = document.createElement('div');
            div.className = 'tip-box';
            div.textContent = tip;
            div.style.backgroundColor = color;

            // 随机位置(确保在可视区内)
            const x = Math.random() * (window.innerWidth - 260);
            const y = Math.random() * (window.innerHeight - 80);
            div.style.left = x + 'px';
            div.style.top = y + 'px';

            document.body.appendChild(div);
        }

        // 每隔 800ms 弹出一个(速度可调)
        setInterval(createTip, 800);

        // 清空按钮功能
        document.getElementById('clearBtn').addEventListener('click', () => {
            document.querySelectorAll('.tip-box').forEach(el => el.remove());
        });
    </script>
</body>
</html>

▶️ 如何运行(Linux 服务器)

bash 复制代码
# 安装依赖
pip3 install flask

# 后台运行
nohup python3 app.py > app.log 2>&1 &

访问 http://你的IP:82,即可看到满屏飘落的温暖小纸条!


二、桌面版:Tkinter 弹窗小纸条(适合本地使用)

这个版本不需要浏览器,直接在桌面弹出窗口,适合送给你身边的人。

📄 warm_desktop.py

python 复制代码
import tkinter as tk
import random

# 所有提示语
tips = [
    '多喝水哦~', '保持微笑呀', '每天都要元气满满',
    '记得吃水果', '保持好心情', '好好爱自己',
    '梦想成真', '期待下一次见面', '恭喜发财',
    '顺顺利利', '早点休息', '愿所有烦恼都消失',
    '别熬夜', '今天过得开心嘛', '天冷了,多穿衣服'
]

bg_colors = [
    'lightpink', 'skyblue', 'lightgreen', 'lavender',
    'lightyellow', 'plum', 'coral', 'bisque', 'aquamarine',
    'mistyrose', 'honeydew', 'lavenderblush', 'oldlace'
]

def create_window():
    window = tk.Toplevel(root)
    window.title('给夏雨的小纸条')

    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    window_width, window_height = 250, 60
    x = random.randint(0, screen_width - window_width)
    y = random.randint(0, screen_height - window_height)
    window.geometry(f"{window_width}x{window_height}+{x}+{y}")


    tip = "夏雨," + random.choice(tips)
    bg = random.choice(bg_colors)

    label = tk.Label(
        window,
        text=tip,
        bg=bg,
        font=('微软雅黑', 14, 'bold'),  # 字体稍调小一点避免换行
        width=30,
        height=3,
        wraplength=230  # 自动换行
    )
    label.pack()

    window.attributes('-topmost', True)


def spawn_windows(count=100, interval=80):  # 100 个测试
    if count > 0:
        create_window()
        root.after(interval, spawn_windows, count - 1, interval)


root = tk.Tk()
root.withdraw()
spawn_windows(100, 80)  # 先弹 100 个,避免卡死
root.mainloop()

如何运行

  • Windows :安装 Python 后,双击 .py 文件即可。

  • Linux

    :确保安装了 GUI 支持(如 Ubuntu Desktop),运行:

    bash 复制代码
    python3 warm_desktop.py

⚠️ 注意:服务器无图形界面时无法运行 Tkinter 版本!


写在最后

编程不只是逻辑与算法,也可以是表达爱的方式

这几十行代码,承载的是一句句说不出口的关心。

你可以:

  • 把网页版部署在云服务器,发链接给 TA;
  • 把桌面版打包成 .exe(用 PyInstaller),悄悄放进 TA 的电脑;
  • 或者,只是自己看着满屏的温暖,治愈一天的疲惫。

愿每一个看到这段代码的人,都被世界温柔以待。


项目开源 :欢迎 改造!
作者 :一位想用代码说"我在乎你"的程序员
日期:2026年1月23日


相关推荐
_codemonster2 小时前
手语识别及翻译项目实战系列(五)整体架构代码详细代码实现
人工智能·python·计算机视觉·架构
Eugene__Chen2 小时前
Java的SPI机制(曼波版)
java·开发语言·python
程序猿20232 小时前
JVM与JAVA
java·jvm·python
独隅2 小时前
本地大模型训练与 API 服务部署全栈方案:基于 Ubuntu 22.04 LTS 的端到端实现指南
服务器·python·语言模型
程序员miki2 小时前
训练yolo11检测模型经验流程
python·yolo
夏了茶糜2 小时前
Python中生成器表达式(generator expression)和列表推导式(list comprehension)的区别
python·列表推导式·生成器表达式
上天夭2 小时前
补充提问(四)
windows·python
0xwang2 小时前
【python01】搭建环境
python
人工智能AI技术3 小时前
【Agent从入门到实践】31 工具调用的核心逻辑:Agent如何选择并执行工具
人工智能·python