Skip to content Skip to sidebar Skip to footer

My Text In My Clock Python Is Not Aligning Properly

My text in my turtle module is not aligning properly, it is aligned up and to the left. I want it to align exactly where the turtle is. Can anyone help? I tried setting the xcor an

Solution 1:

A simpler, though potentially less accurate, way to do this completely within turtle:

FONT_SIZE = 16
FONT = ('Times New Roman', FONT_SIZE)

t.color('red')
t.dot(2) # show target ofwhere we want to center text, for debugging
t.color('black')

t.sety(t.ycor() - FONT_SIZE/2)
t.write(12, align='center', font=FONT)

enter image description here

Now let's address your program as a whole. The primary issues I see is that it flickers and is more complicated than necessary. The first thing to do is to switch turtle into Logo mode, which makes positive angles clockwise and makes 0 degrees at the top (not unlike a clock!).

Then we split the dial drawing onto it's own turtle to be drawn once an we put the hands on their own turtle to be erased and redraw over and over. We all toss the while True: and sleep(), which have no place in an event-driven world like turtle, and use a turtle timer event instead:

from datetime import datetime
from turtle import Screen, Turtle

OUTER_RADIUS = 150
LARGE_TICK = 20
SMALL_TICK = 10

FONT_SIZE = 16
FONT = ('Times New Roman', FONT_SIZE)

defdraw_dial():
    dial = Turtle()
    dial.hideturtle()

    dial.dot()

    dial.up()
    dial.forward(OUTER_RADIUS)
    dial.right(90)
    dial.down()
    dial.circle(-OUTER_RADIUS)
    dial.up()
    dial.left(90)
    dial.backward(OUTER_RADIUS)

    for mark inrange(60):
        distance = LARGE_TICK if mark % 5 == 0else SMALL_TICK
        dial.forward(OUTER_RADIUS)
        dial.down()
        dial.backward(distance)
        dial.up()
        dial.backward(OUTER_RADIUS - distance)
        dial.right(6)

    dial.sety(-FONT_SIZE/2)
    dial.setheading(30)  # starting at 1 o'clockfor z inrange(1, 13):
        dial.forward(OUTER_RADIUS - (LARGE_TICK + FONT_SIZE/2))
        dial.write(z, align='center', font=FONT)
        dial.backward(OUTER_RADIUS - (LARGE_TICK + FONT_SIZE/2))
        dial.right(30)

deftick():
    hour_hand = datetime.today().hour
    minute_hand = datetime.today().minute
    second_hand = datetime.today().second

    hands.reset()
    hands.hideturtle()  # redo as undone by reset()

    hands.right(hour_hand * 30 + minute_hand / 60 * 30)
    hands.forward(1/3 * OUTER_RADIUS)
    hands.backward(1/3 * OUTER_RADIUS)
    hands.left(hour_hand * 30 + minute_hand / 60 * 30)

    hands.right(minute_hand * 6)
    hands.forward(2/3 * OUTER_RADIUS)
    hands.backward(2/3 * OUTER_RADIUS)
    hands.left(minute_hand * 6)

    hands.right(second_hand * 6)
    hands.forward(OUTER_RADIUS - (LARGE_TICK + FONT_SIZE))
    hands.backward(OUTER_RADIUS - (LARGE_TICK + FONT_SIZE))
    hands.left(second_hand * 6)

    screen.update()
    screen.ontimer(tick, 1000)

screen = Screen()
screen.mode('logo')  # make 0 degrees straight up, positive angles clockwise (like a clock!)
screen.tracer(False)

draw_dial()

hands = Turtle()

tick()

screen.mainloop()

enter image description here

Solution 2:

I don't really want to use tkinter, it is too complicated.

I don't think it's too complicated and it is the most straight-forward solution to center the text vertically.

Use the following code to get the height of text in your font:

from tkinter importfontt= turtle.Pen()

font_config = font.Font(font=('Times New Roman', 16))
font_height = font_config.metrics("linespace")

Then draw your text using font_height/2 as the vertical offset:

        t.sety(t.ycor() - font_height/2)
        t.write(z, align="center", font=font_config)
        t.sety(t.ycor() + font_height/2)

Post a Comment for "My Text In My Clock Python Is Not Aligning Properly"