How To Center Text Using "turtle" Module In Python
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 wi
Solution 1:
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()
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.
Post a Comment for "How To Center Text Using "turtle" Module In Python"