0

タートル モジュールのテキストが正しく配置されていません。左上に配置されています。カメがいる場所に正確に合わせたい。誰でも助けることができますか?タートルの xcor と ycor を上と左に 5 単位ずつ設定しようとしましたが、うまくいきませんでした。どんな助けでも大歓迎です。

コード:

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 を使いたくありません。複雑すぎます。

4

2 に答える 2

3

タートル内でこれを完全に行うためのより簡単な方法ですが、正確性は劣​​る可能性があります。

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)

ここに画像の説明を入力

それでは、プログラム全体に取り組みましょう。私が目にする主な問題は、ちらつきと必要以上に複雑なことです。最初に行うことは、タートルをロゴモードに切り替えることです。これにより、正の角度が時計回りになり、上部が 0 度になります (時計とは異なります!)。

次に、文字盤の描画を自分のタートルに分割して一度描画し、自分のタートルに手を置いて消去し、何度も再描画します。while True:タートルのようなイベント駆動型の世界では場所がないandを投げて、sleep()代わりにタートル タイマー イベントを使用します。

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()

ここに画像の説明を入力

于 2020-05-31T17:37:01.760 に答える
2

私は本当に tkinter を使いたくありません。複雑すぎます。

複雑すぎるとは思いません。テキストを垂直方向に中央揃えするのが最も簡単な解決策です。

次のコードを使用して、フォント内のテキストの高さを取得します。

from tkinter import font

t = turtle.Pen()

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

font_height/2次に、垂直オフセットとして使用してテキストを描画します。

        t.sety(t.ycor() - font_height/2)
        t.write(z, align="center", font=font_config)
        t.sety(t.ycor() + font_height/2)
于 2020-05-31T17:03:26.443 に答える