Python Forum

Full Version: Turtle.setpos() vs text position on screen query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The program draws three centered lines on the screen and writes text at the same Y position. The text floats above the line with the spaceing between the line and the text varing with the font size. Decenders do not reach down to the line.

Same result with Thonny on Windows or Raspberry Pi.

What is the relationship between the Y turtle position, font size, and where turtle.write() puts text on the screen? Does each character exist in a box of HxW pixels related to the font size? Where would I find this info in the documentation?

from turtle import *
import turtle as tur

#Set various parameters
ypos = (200, 0, -200) # Line and text vertical position
fntsz = (20, 35, 50) #Font size
clr = ('red', 'green', 'blue')

for i in range(3):
    #Draw a reference line
    y = ypos[i]
    tclr = clr[i]
    sz = fntsz[i]
    style = ('courier', sz, 'bold')
    penup()
    tur.setpos(-100,y)
    tur.color(tclr)
    pendown()
    forward(200)
    penup()
#Return to the same Y position
#Center X to match the text
    tur.setpos(0,y)
    teststring = 'Test String ' + str(i) #Create a string
    pendown() #And write the text
    tur.write(teststring, font = style, align = 'center')
    penup()
Thanks for your help.