如何使用“turtle"将文本居中Python中的模块 [英] How to center text using "turtle" module in Python
问题描述
如何从窗口中心开始绘制turtle"文本?我知道如何创建海龟"文本,但我不知道如何将文本居中,使其从窗口中心显示.
How can I draw "turtle" text starting from the center of a window? I know how to create "turtle" text, but I don't know how to center the text so it appears from the center of a window.
turtle.write("I AM HERE", font=("Arial", 50, "bold"))
感谢您的帮助,
霍华德
推荐答案
要将文本居中于一个点(例如原点在 [0, 0]),在 X 维度上,您可以使用 align=center
turtle.write()
的关键字参数.要在 Y 维度上对齐,您需要稍微调整字体大小:
To center text on a point (e.g. the origin at [0, 0]), in the X dimension, you can use the align=center
keyword argument to turtle.write()
. To get alignment in the Y dimension, you need to adjust slightly for the font size:
from turtle import Turtle, Screen
FONT_SIZE = 50
FONT = ("Arial", FONT_SIZE, "bold")
yertle = Turtle()
# The turtle starts life in the center of the window
# but let's assume we've been drawing other things
# and now need to return to the center of the window
yertle.penup()
yertle.home()
# By default, the text prints too high unless we roughly
# readjust the baseline, you can confirm text placement
# by doing yertle.dot() after yertle.home() to see center
yertle.sety(-FONT_SIZE/2)
yertle.write("I AM HERE", align="center", font=FONT)
yertle.hideturtle()
screen = Screen()
screen.exitonclick()
但是,如果您想从中心开始打印(您的帖子不清楚),您可以将 align=center
更改为 align=left
,或者只是完全省略 align
关键字参数.
However if you instead want to start printing from the center (your post isn't clear), you can change align=center
to align=left
, or just leave out the align
keyword argument altogether.
这篇关于如何使用“turtle"将文本居中Python中的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!