栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我的时钟python中的文本未正确对齐

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我的时钟python中的文本未正确对齐

一种完全但不完全准确的方法,尽管可能不太准确:

FONT_SIZE = 16FONT = ('Times New Roman', FONT_SIZE)t.color('red')t.dot(2) # show target of where we want to center text, for debuggingt.color('black')t.sety(t.ycor() - FONT_SIZE/2)t.write(12, align='center', font=FONT)

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

然后, 一旦 将表盘放在自己的乌龟上进行擦除并一遍又一遍地绘制, 就将 表盘图分割到自己的乌龟上进行绘制。我们都将

whileTrue:
and抛弃
sleep()
在事件驱动的世界(如turtle)中没有位置,而是使用turtle timer事件:

from datetime import datetimefrom turtle import Screen, TurtleOUTER_RADIUS = 150LARGE_TICK = 20SMALL_TICK = 10FONT_SIZE = 16FONT = ('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()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/661477.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号