我的时钟python中的文本未正确对齐 [英] My text in my clock python is not aligning properly

查看:86
本文介绍了我的时钟python中的文本未正确对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

乌龟模块中的文本未正确对齐,它的位置是向上和向左对齐的.我希望它准确对准乌龟的位置.有人可以帮忙吗?我尝试将乌龟的xcor和ycor向上和向左设置5个单位,但这没有用.任何帮助将不胜感激.

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 and ycor of the turtle up and to the left by 5 units and that did not work. Any help would be greatly appreciated.

代码:

import time
from datetime import datetime,date
import turtle
t = turtle.Pen()

while True:
    turtle.tracer(0, 0)
    hour_hand = float(datetime.today().hour)
    minute_hand = float(datetime.today().minute)
    second_hand = float(datetime.today().second)

    # Draw circle
    t.hideturtle()
    t.circle(150)
    t.left(90)
    t.up()
    t.forward(150)
    t.down()

    # Draw hands
    t.right(float(float(minute_hand) * 6))
    t.forward(100)
    t.backward(100)
    t.left(float(float(minute_hand) * 6))
    t.right(int(float(hour_hand) * 30 + float(minute_hand) / 60 * 30))
    t.forward(50)
    t.backward(50)
    t.left(int(float(hour_hand) * 30 + float(minute_hand) / 60 * 30))
    t.right(second_hand * 6)
    t.forward(125)
    t.backward(125)
    t.left(second_hand * 6)

    # Draw ticks
    for x in range(0, 12):
        t.up()
        t.forward(130)
        t.down()
        t.forward(20)
        t.backward(20)
        t.up()
        t.backward(130)
        t.down()
        t.right(30)
    for y in range(0, 60):
        t.up()
        t.forward(140)
        t.down()
        t.forward(10)
        t.backward(10)
        t.up()
        t.backward(140)
        t.down()
        t.right(6)
    t.up()

    # Draw numbers
    t.right(32.5)
    for z in range(1, 12):
        t.forward(130)
        t.sety(t.ycor() - 5)
        t.setx(t.xcor() - 5)
        t.write(z, align = 'center', font = ('Times New Roman', 16))
        t.sety(t.ycor() + 5)
        t.setx(t.xcor() + 5)
        t.backward(130)
        t.right(30)
    t.forward(130)
    t.write(12, align = 'center', font = ('Times New Roman', 16))
    turtle.update()
    t.hideturtle()
    time.sleep(0.85)
    t.reset()

我真的不想使用tkinter,它太复杂了.

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

推荐答案

一种更简单但可能不太准确的方法,可以完全在turtle中完成此操作:

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 of where we want to center text, for debugging
t.color('black')

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

现在,让我们整体上解决您的程序.我看到的主要问题是它闪烁并且比必要的更为复杂.首先要做的是将乌龟切换到 Logo 模式,该模式将顺时针旋转为正角,顶部旋转为0度(与时钟不同!).

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!).

然后,将表盘图分割到要绘制的自己的乌龟上 ,然后将手放在要擦除的自己的乌龟上,并一遍又一遍地重新绘制.我们都将while True:sleep()扔掉,它们在像乌龟这样的事件驱动世界中没有位置,而是使用乌龟计时器事件:

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)

def draw_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 in range(60):
        distance = LARGE_TICK if mark % 5 == 0 else 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'clock

    for z in range(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)

def tick():
    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()

这篇关于我的时钟python中的文本未正确对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆