今天是520,作为程序员,我们不妨用turtle来表达我们的浪漫。让我们来看看如何用代码绘制一个爱心,并在其中写上"520"。
程序员的浪漫,提前预览:

以下是实现这个效果的代码:
# 导入需要用到的模块 import turtle as tt import time画爱心的顶部
def LittleHeart(): for i in range(200): tt.right(1) tt.forward(2)
love = '520'
窗口大小
tt.setup(width=800, height=500)
颜色
tt.color('red', 'pink')
笔粗细
tt.pensize(5)
速度
tt.speed(2)
提笔
tt.up()
隐藏笔
tt.hideturtle()
去到的坐标,窗口中心为0,0
tt.goto(0, -180) tt.showturtle()
画上线
tt.down() tt.speed(1) tt.begin_fill() tt.left(140) tt.forward(224)
调用画爱心左边的顶部
LittleHeart()
调用画爱右边的顶部
tt.left(120) LittleHeart()
画下线
tt.forward(224) tt.end_fill()
tt.pensize(5) tt.up() tt.hideturtle()
在心中写字 一次
tt.goto(0, 0) tt.showturtle() tt.color('#CD5C5C', 'pink')
在心中写字 font可以设置字体自己电脑有的都可以设 align开始写字的位置
tt.write(love, font=('kaiti', 40,), align="center") tt.up() tt.hideturtle()
time.sleep(2)
在心中写字 二次
tt.goto(0, 0) tt.showturtle() tt.color('red', 'pink') tt.write(love, font=('kaiti', 40,), align="center") tt.up() tt.hideturtle()
点击窗口关闭
window = tt.Screen() window.exitonclick()
在安装turtle模块时,如果你是Windows用户,可能会遇到以下问题:
(studyenv) E:\working\GBase\django_orm_48s\codespace\django_gbasedbt>pip install turtle Collecting turtle
Downloading turtle-0.0.2.tar.gz (11 kB)
ERROR: Command errored out with exit status 1:
command: 'E:\devlop\envs\anaconda\envs\studyenv\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\dafei\AppData\Local\Temp\pip-install-i3wdw9zc\turtle_bb87823cd0bf40c7b00659bf322275b4\setup.py'"'"'; file='"'"'C:\Users\dafei\AppData\Local\Temp\pip-install-i3wdw9zc\turtle_bb87823cd0bf40c7b00659bf322275b4\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\dafei\AppData\Local\Temp\pip-pip-egg-info-haf9ipy6'
cwd: C:\Users\dafei\AppData\Local\Temp\pip-install-i3wdw9zc\turtle_bb87823cd0bf40c7b00659bf322275b4\
Complete output (6 lines):
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\dafei\AppData\Local\Temp\pip-install-i3wdw9zc\turtle_bb87823cd0bf40c7b00659bf322275b4\setup.py", line 40
except ValueError, ve: ^
SyntaxError: invalid syntaxERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
解决这个问题的方法是下载turtle的安装包,并修改setup.py文件中的第40行,将except ValueError, ve:改为except ValueError as ve:,然后再进行安装。











