简体   繁体   English

无法让海龟 ontimer() function 工作

[英]Can't get turtle ontimer() function working

I can't get turtle ontimer() function working.我无法让海龟 ontimer() function 工作。

When I use the ontimer function of the turtle library the code executes directly and does not wait for the expected time.当我使用turtle库的ontimer function时,代码直接执行,不等待预期时间。

To make my code work I used the function time.sleep(1) which is not a great solution为了使我的代码正常工作,我使用了 function time.sleep(1) 这不是一个很好的解决方案

Here is the code:这是代码:

import turtle, time

heure_=int(input("Enter the time in hh format: "))
minute_=int(input("Enter the minutes in mm format: "))
seconde_=int(input("Enter the seconds in ss format: "))

#calculate time
def formt(h, m, s):
    minute, seconde = divmod(s, 60)
    heure, minute = divmod(minute+m, 60)
    heure = (heure+h)%12
    return heure, minute, seconde

heure, minute, seconde = formt(heure_, minute_, seconde_)
#function to calculate hands angles
def angle(h, m, s):
    angle_s=(360 / 60) * s
    angle_m=(360 / 60) * m + angle_s*1/60
    angle_h=(360 / 12) * h + angle_m*1/12
    return angle_h, angle_m, angle_s

#hands
def aigh(h, x=0, y=0, f=100):
    hour_t.ht()
    hour_t.goto(x,y)
    hour_t.setheading(90)
    hour_t.rt(h)
    hour_t.pendown()
    hour_t.width(10)
    hour_t.forward(f)

def aigm(m, x=0, y=0, f=150):
    min_t.ht()
    min_t.goto(x,y)
    min_t.setheading(90)
    min_t.rt(m)
    min_t.pendown()
    min_t.width(5)
    min_t.forward(f)

def aigs(s, x=0, y=0, f=170):
    sec_t.ht()
    sec_t.goto(x,y)
    sec_t.setheading(90)
    sec_t.color("red")
    sec_t.width()
    sec_t.rt(s)
    sec_t.pendown()
    sec_t.width(1)
    sec_t.forward(f)

#init turtle
t = turtle.Turtle()

#turtles for hands
hour_t = turtle.Turtle()
min_t = turtle.Turtle()
sec_t = turtle.Turtle()

wn = turtle.Screen()
wn.title("Clock")

wn.tracer(0)

def draw(h, m, s):
    aigh(h)
    aigm(m)
    aigs(s)
    
def refresh(h=heure, m=minute, s=seconde):
    global seconde
    wn.tracer(0)
    hour_t.undo()
    min_t.undo()
    sec_t.undo()
    
    h_, m_, s_ = formt(h, m, s)
    t.undo()
    
    #get the hands angles
    agl_h, agl_m, agl_s = angle(h_, m_, s_)

    #draw the hands
    draw(agl_h, agl_m, agl_s)

    wn.update()
    
    seconde +=1

    #wn.ontimer(refresh, 1000)

for _ in iter(int, 1):
    refresh(s=seconde)
    time.sleep(1)

How can i make the code work?我怎样才能使代码工作? Thank you.谢谢你。

You have fallen into one of the classic Python traps.您已陷入经典的 Python 陷阱之一。 When you say this:当你这样说时:

def refresh(h=heure, m=minute, s=seconde):

The default values are captured at the time the function is defined .默认值是在定义 function 时捕获的 Those are not live values.这些不是实时值。 So, if you enter 5, 15, 23, the default values for those parameters will ALWAYS be 5, 15 and 23, regardless of how the globals change.因此,如果您输入 5、15、23,这些参数的默认值将始终为 5、15 和 23,无论全局变量如何变化。

You are totally dependent on the globals here, so just use them.您完全依赖于这里的全局变量,所以只需使用它们。 This works:这有效:

import turtle, time

heure_=int(input("Enter the time in hh format: "))
minute_=int(input("Enter the minutes in mm format: "))
seconde_=int(input("Enter the seconds in ss format: "))

#calculate time
def formt(h, m, s):
    minute, seconde = divmod(s, 60)
    heure, minute = divmod(minute+m, 60)
    heure = (heure+h)%12
    return heure, minute, seconde

heure, minute, seconde = formt(heure_, minute_, seconde_)
#function to calculate hands angles
def angle(h, m, s):
    angle_s=(360 / 60) * s
    angle_m=(360 / 60) * m + angle_s*1/60
    angle_h=(360 / 12) * h + angle_m*1/12
    return angle_h, angle_m, angle_s

#hands
def aigh(h, x=0, y=0, f=100):
    hour_t.ht()
    hour_t.goto(x,y)
    hour_t.setheading(90)
    hour_t.rt(h)
    hour_t.pendown()
    hour_t.width(10)
    hour_t.forward(f)

def aigm(m, x=0, y=0, f=150):
    min_t.ht()
    min_t.goto(x,y)
    min_t.setheading(90)
    min_t.rt(m)
    min_t.pendown()
    min_t.width(5)
    min_t.forward(f)

def aigs(s, x=0, y=0, f=170):
    sec_t.ht()
    sec_t.goto(x,y)
    sec_t.setheading(90)
    sec_t.color("red")
    sec_t.width()
    sec_t.rt(s)
    sec_t.pendown()
    sec_t.width(1)
    sec_t.forward(f)

#init turtle
t = turtle.Turtle()

#turtles for hands
hour_t = turtle.Turtle()
min_t = turtle.Turtle()
sec_t = turtle.Turtle()

wn = turtle.Screen()
wn.title("Clock")

wn.tracer(0)

def draw(h, m, s):
    aigh(h)
    aigm(m)
    aigs(s)
    
def refresh():
    global seconde
    wn.tracer(0)
    hour_t.undo()
    min_t.undo()
    sec_t.undo()
    
    h_, m_, s_ = formt(heure, minute, seconde)
    t.undo()
    
    #get the hands angles
    agl_h, agl_m, agl_s = angle(h_, m_, s_)

    #draw the hands
    draw(agl_h, agl_m, agl_s)

    wn.update()
    
    seconde +=1

while True:
    refresh()
    time.sleep(1)

I see a number of small issues with your code (eg the hands don't take on their assigned widths and colors.) I agree with @TimRoberts that default arguments are your primary issue.我看到您的代码存在一些小问题(例如,手不采用指定的宽度和 colors。)我同意@TimRoberts 的观点,默认 arguments 是您的主要问题。 Below is my rework of the code to use ontimer() and attempt to both fix and simplify it:下面是我对使用ontimer()的代码的修改,并尝试修复和简化它:

from turtle import Screen, Turtle

heure_ = int(input("Enter the time in hh format: "))
minute_ = int(input("Enter the minutes in mm format: "))
seconde_ = int(input("Enter the seconds in ss format: "))

# calculate time
def formt(h, m, s):
    minute, seconde = divmod(s, 60)
    heure, minute = divmod(minute + m, 60)
    heure = (heure + h) % 12

    return heure, minute, seconde

# function to calculate hands angles
def angle(h, m, s):
    angle_s = (360 / 60) * s
    angle_m = (360 / 60) * m + angle_s/60
    angle_h = (360 / 12) * h + angle_m/12

    return angle_h, angle_m, angle_s

# hands
def aigh(h, x=0, y=0, f=100):
    hour_t.goto(x, y)
    hour_t.clear()
    hour_t.setheading(h)
    hour_t.forward(f)

def aigm(m, x=0, y=0, f=150):
    min_t.goto(x, y)
    min_t.clear()
    min_t.setheading(m)
    min_t.forward(f)

def aigs(s, x=0, y=0, f=170):
    sec_t.goto(x, y)
    sec_t.clear()
    sec_t.setheading(s)
    sec_t.forward(f)

def draw(h, m, s):
    aigh(h)
    aigm(m)
    aigs(s)

heure, minute, seconde = formt(heure_, minute_, seconde_)

def refresh():
    global heure, minute, seconde

    heure, minute, seconde = formt(heure, minute, seconde)

    # get the hands angles
    agl_h, agl_m, agl_s = angle(heure, minute, seconde)

    # draw the hands
    draw(agl_h, agl_m, agl_s)

    seconde += 1

    screen.update()

    screen.ontimer(refresh, 1000)

screen = Screen()
screen.mode('logo')  # 0 degrees at top, circles go clockwise, like a clock!
screen.title("Clock")
screen.tracer(False)

# turtles for hands
hour_t = Turtle()
hour_t.hideturtle()
hour_t.width(10)

min_t = Turtle()
min_t.hideturtle()
min_t.width(5)

sec_t = Turtle()
sec_t.hideturtle()
sec_t.width(1)
sec_t.color("red")

refresh()

screen.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM