[Python] 借助 turtle 逐字展示唐诗《金缕衣》

背景

在 Python 借助 Turtle 逐字展示《醉翁亭记》和《兰亭集序》 一文中,我们探索了如何借助 turtle --- Turtle graphics 展示《醉翁亭记》《兰亭集序》之类的经典古文。由此,我联想到,还可以用它来逐字展示唐诗。

正文:逐字展示《金缕衣》

代码

完整的代码如下 ⬇️

python 复制代码
import math
import time
from turtle import Screen, Turtle

ARTICLE = (
    "勸君莫惜金縷衣"
    "勸君惜取少年時"
    "花開堪折直須折"
    "莫待無花空折枝"
)

CHAR_SIZE = 28
CHAR_WIDTH = 40
CHARS_PER_COLUMN = 7
COLUMN_SPACING = 48
MARGIN = 80

CHAR_DELAY_MS = 150


def load_article():
    return ARTICLE


def draw_char(x, y, ch, pen):
    pen.penup()
    pen.goto(x, y)
    pen.pendown()
    font = ("LXGWWenKai-Regular", CHAR_SIZE, "normal")
    pen.write(ch, align="center", font=font)


def setup_screen(width, height):
    screen = Screen()
    screen.setup(width, height)
    screen.title("金縷衣")
    screen.tracer(0)
    return screen


def setup_pen():
    pen = Turtle()
    pen.hideturtle()
    pen.color("black")
    return pen


def draw_next_char(position, characters, screen, pen, start_x, start_y):
    col = position // CHARS_PER_COLUMN
    row = position % CHARS_PER_COLUMN

    x = start_x - col * COLUMN_SPACING
    y = start_y - row * CHAR_WIDTH

    ch = characters[position]
    draw_char(x, y, ch, pen)
    screen.update()


def main():
    characters = list(load_article())

    total = len(characters)
    column_count = math.ceil(total / CHARS_PER_COLUMN)
    content_width = (column_count - 1) * COLUMN_SPACING
    content_height = (CHARS_PER_COLUMN - 1) * CHAR_WIDTH + CHAR_SIZE

    screen_width = content_width + 2 * MARGIN
    screen_height = content_height + 2 * MARGIN

    start_x = screen_width // 2 - MARGIN
    start_y = screen_height // 2 - MARGIN - CHAR_SIZE

    screen = setup_screen(screen_width, screen_height)
    pen = setup_pen()

    for position in range(total):
        draw_next_char(position, characters, screen, pen, start_x, start_y)
        time.sleep(CHAR_DELAY_MS / 1000)

    screen.mainloop()


if __name__ == "__main__":
    main()

请将上述代码保存为 jin.py,用如下的命令可以运行 jin.py

bash 复制代码
python3 jin.py

运行的效果如下图所示 ⬇️

注意事项

如果您在运行时遇到了报错,可以从以下方面进行排查

其他

将代码稍作修改,就可以展示其他唐诗。需要修改的地方如下 ⬇️

例如以下代码可以逐字展示唐诗《江雪》

python 复制代码
import math
import time
from turtle import Screen, Turtle

ARTICLE = (
    "千山鳥飛絕"
    "萬徑人蹤滅"
    "孤舟簑笠翁"
    "獨釣寒江雪"
)

CHAR_SIZE = 28
CHAR_WIDTH = 40
CHARS_PER_COLUMN = 5
COLUMN_SPACING = 48
MARGIN = 80

CHAR_DELAY_MS = 150


def load_article():
    return ARTICLE


def draw_char(x, y, ch, pen):
    pen.penup()
    pen.goto(x, y)
    pen.pendown()
    font = ("LXGWWenKai-Regular", CHAR_SIZE, "normal")
    pen.write(ch, align="center", font=font)


def setup_screen(width, height):
    screen = Screen()
    screen.setup(width, height)
    screen.title("江雪")
    screen.tracer(0)
    return screen


def setup_pen():
    pen = Turtle()
    pen.hideturtle()
    pen.color("black")
    return pen


def draw_next_char(position, characters, screen, pen, start_x, start_y):
    col = position // CHARS_PER_COLUMN
    row = position % CHARS_PER_COLUMN

    x = start_x - col * COLUMN_SPACING
    y = start_y - row * CHAR_WIDTH

    ch = characters[position]
    draw_char(x, y, ch, pen)
    screen.update()


def main():
    characters = list(load_article())

    total = len(characters)
    column_count = math.ceil(total / CHARS_PER_COLUMN)
    content_width = (column_count - 1) * COLUMN_SPACING
    content_height = (CHARS_PER_COLUMN - 1) * CHAR_WIDTH + CHAR_SIZE

    screen_width = content_width + 2 * MARGIN
    screen_height = content_height + 2 * MARGIN

    start_x = screen_width // 2 - MARGIN
    start_y = screen_height // 2 - MARGIN - CHAR_SIZE

    screen = setup_screen(screen_width, screen_height)
    pen = setup_pen()

    for position in range(total):
        draw_next_char(position, characters, screen, pen, start_x, start_y)
        time.sleep(CHAR_DELAY_MS / 1000)

    screen.mainloop()


if __name__ == "__main__":
    main()

运行的效果如下图所示

维机文库 中提供了 唐詩三百首 的链接,从中也许可以找到您感兴趣的唐诗

参考资料

相关推荐
the局外人2 分钟前
FastAPI中jwt的基本使用与实践
后端·python·fastapi
mklpo14716 分钟前
为不支持sse4.2命令集的老旧设备制作Win11 26H2系统镜像,2026-10-11。
运维·windows·python
GoldenSpider.AI20 分钟前
用 AI 改流程之前先做「四桶」体检:9 次返工里 8 次靠规则就能消掉(附 Python 发布前校验器)
人工智能·python·自动化·ai智能体
小白快快跑哦30 分钟前
python-字符串全解(六):正则表达式-修饰符
python·正则表达式·修饰符
niucloud-admin1 小时前
JAVA V6 多商户商城 开发文档——项目启动
java·开发语言·python
CCIE-Yasuo1 小时前
个人电脑搭建AI-Qwen3-8B-学习记录
人工智能·python·学习
小猴子爱上树1 小时前
跨马翻译:批量图片翻译+视频字幕+智能抠图,跨境电商翻译工具
python·音视频
棉猴2 小时前
玩游戏学Python2-IDLE与pgzero入门
开发语言·python·游戏·玩游戏·pgzero·pgzrun
TechEdu2026062 小时前
[人工智能]Python12: SciPy 实战指南
人工智能·python·scipy
jack186892 小时前
Github Copilot 高效开发实战场景指南
python