Thirth Patel
Thirth Patel

Reputation: 31

How can I position text in Python Turtle?

For example:

write("First Last", True, align="right")

But with specific (x,y) coordinates

Upvotes: 3

Views: 25975

Answers (2)

Partha
Partha

Reputation: 11

Before writing the text to the screen, you can hide the turtle, penup and move the turtle to a specific position.

import turtle
import datetime

turt = turtle.Turtle()

today = datetime.datetime.now()
turt.hideturtle()
turt.penup()
#Take the turtle to left edge of the screen
turt.backward((turt.getscreen().window_width() / 2) - 10)
message = "Hello Turtle! \nToday is " + today.strftime("%A") + ', ' + today.strftime("%d") \
           + ', ' + today.strftime("%B") + ', ' + today.strftime("%Y") 

turt.write(message,move=False, font=('monaco',30,'bold'),align='left')
turtle.done()

Upvotes: 1

cdlane
cdlane

Reputation: 41872

Along with using a command like setposition() aka goto(), setx() or sety() to position your turtle before you call write(), the move parameter to write(), which you set to True, doesn't do much when right aligning a left to right language like English, so you might as well leave it out:

setposition(100, 150)

write("First Last", align="right")

This will write the text "First Last", right aligned to the point (100, 150). I.e. the end of the text will fall at that position.

Upvotes: 2

Related Questions