我的好心情

博主心情不错,希望以后生活更坚强。看到别人写的程序,赞叹之余也希望自己能写出高质量程序,想让自己在竞争中不断提升编程能力。

今天,心情不错,不过还是有点感性.希望自己在以后的生活要更加的坚强.

看见别人写的程序,我不赞叹也不行啊.希望自己也可以写出高质量的程序来.^_^

这样最好不过了,可以自己学习的强烈欲望.让自己处于那种激烈的竞争中,不断提升自己的能力.

好的.就此暂停.以后再写了.

# 代码概述 该文件为一段使用 Python 的 `tkinter` 库编写的图形界面代码,结合了数学函数绘制爱心形状,并通过多个弹窗展示温馨祝福语。整体功能是创建一系列浮动子窗口,在屏幕上动态显示祝福语和一个逐步绘制的爱心动画。 尽管原文本存在大量乱码与字符错乱(如 `imp=tmco3ag=bo` 应为 `import`,`r-andicm` 应为 `random`),但可通过上下文还原其核心逻辑。 --- # 代码解析 以下是修复并整理后的完整可运行代码: ```python import tkinter as tk import random import math # 主窗口初始化 root = tk.Tk() root.withdraw() # 隐藏主窗口 # 存储所有弹窗 popups = [] # 屏幕尺寸 screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() # 窗口尺寸 window_width = 182 window_height = 80 # 心形轨迹生成 def heart_shape(): points = [] for theta in range(0, 360, 2): # 每2度取一点 x = 16 * (math.sin(math.radians(theta))) ** 3 y = 13 * math.cos(math.radians(theta)) \ - 5 * math.cos(2 * math.radians(theta)) \ - 2 * math.cos(3 * math.radians(theta)) \ - math.cos(4 * math.radians(theta)) # 放大并平移到屏幕中心 offset_x = 400 offset_y = 300 screen_x = offset_x + x * 10 screen_y = offset_y - y * 10 # y方向翻转 points.append((screen_x, screen_y)) return points # 显示单个祝福弹窗 def show_tip(index): x = random.randint(50, screen_width - window_width) y = max(0, min(random.randint(50, screen_height - window_height), screen_height - window_height)) window = tk.Toplevel(root) window.title('温馨提示') window.geometry(f"{window_width}x{window_height}+{int(x)}+{int(y)}") window.resizable(False, False) window.attributes('-topmost', True) # 置顶 if index == 199: tip_text = "永远爱你" bg_color = "lightpink" else: tips = [ '多喝水哦~', '保持微笑呀', '每天都要元气满满,记得吃水果', '保持好心情', '好好爱自己,我想你了,梦想成真', '期待下一次见面', '金榜题名', '顺顺利利', '早点休息', '愿所有烦恼都消失', '别熬夜,今天过得开心嘛', '天冷了,多穿衣服', '你是最棒的', '记得按时吃饭', '今天也要加油呀', '注意保护眼睛', '相信自己', '一切都会好的', '保持乐观心态', '记得运动一下' ] tip_text = random.choice(tips) r = random.randint(50, 255) g = random.randint(50, 255) b = random.randint(50, 255) bg_color = f'#{r:02x}{g:02x}{b:02x}' label = tk.Label(window, text=tip_text, font=('楷体', 10), bg=bg_color, wraplength=160) label.pack(padx=20, pady=20) popups.append(window) # 绘制爱心并显示祝福 def start_animation(): heart_points = heart_shape() for i, (x, y) in enumerate(heart_points): color = f'#{random.randint(50,255):02x}{random.randint(50,255):02x}{random.randint(50,255):02x}' dot = tk.Canvas(root, width=10, height=10, bg=color, highlightthickness=0) dot.place(x=x, y=y) root.update() root.after(50) # 控制动画速度 # 所有点绘制完成后显示多个祝福窗口 for idx in range(20): show_tip(idx) # 关闭所有弹窗 def close_all_popups(): for popup in popups: if isinstance(popup, tk.Toplevel) and popup.winfo_exists(): popup.destroy() popups.clear() # 启动动画 root.after(1000, start_animation) # 运行主循环 root.mainloop() ``` --- # 知识点 1. **`tkinter` 图形界面编程**:Python 标准库,用于构建 GUI,支持窗口、标签、画布、事件循环等基础组件。 2. **参数方程绘制几何图形**:通过三角函数组合定义心形曲线 $ x(\theta), y(\theta) $,实现数学艺术图形生成。 3. **动态动画与随机控制**:使用 `after()` 实现延迟绘制,配合 `random` 设置颜色与位置,增强视觉交互体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值