李峋爱心代码
时间: 2025-04-26 15:13:22 浏览: 19
### 李峋同款可写字版跳动的爱心代码
为了实现李峋同款可写字版跳动的爱心,在 Python 中可以借助 `tkinter` 和数学公式共同完成这一目标。具体来说,心形图案可以通过参数方程定义:
\[ x(t) = 16 \sin^3(t), y(t) = 13\cos(t)-5\cos(2t)-2\cos(3t)-\cos(4t) \]
其中 \( t \in [0, 2\pi] \)[^1]。
下面是一个简单的例子展示如何使用上述方程创建静态的心形图像并允许在其上书写文字的功能:
```python
import tkinter as tk
from math import pi, sin, cos
def heart_shape(canvas):
points = []
for i in range(0, 360 * 4): # 更精细地描绘心脏线
t = (i / 160) * pi
x = int(16 * pow(sin(t), 3))
y = -int((13 * cos(t) - 5 * cos(2*t) - 2 * cos(3*t) - cos(4*t)))
canvas.create_oval(x + 150, y + 150, x + 152, y + 152, fill='red', outline='') # 移动到画布中心位置
points.append([x + 150, y + 150])
return points
root = tk.Tk()
cv = tk.Canvas(root, width=300, height=300)
cv.pack()
heart_points = heart_shape(cv)
entry_text = tk.StringVar()
text_entry = tk.Entry(root, textvariable=entry_text).pack()
def draw_text(event=None):
cv.delete('all')
heart_shape(cv)
input_txt = entry_text.get()
cv.create_text(150, 280, font=("Purisa", 16), text=input_txt)
button_show_text = tk.Button(root, command=draw_text, text="Show Text").pack()
root.mainloop()
```
此程序首先绘制了一个红色的心脏线条图,接着提供了一个输入框让用户能够自定义想要显示的文字内容,并将其放置于画面底部中央的位置[^1]。
阅读全文
相关推荐


















