在两个连续执行的函数之间添加延迟

Add delays between two successively executed functions

我想,如果用户连续调用两个函数,两个函数之间会有某种类型的 "delay",这样当第一个调用的函数是执行。也许像队列这样的东西会做。为了形象化我在说什么,这里是我要调用的函数,根据按键,在海龟图形中绘制相应的字母 window:

def draw_H():
    # Draw the left leg of H.
    # The turtle starts at the bottom left of the letter, pointing right.
    left(90)
    forward(letter_height)
    # Draw the bar of the H.
    # The turtle starts at the top of the left leg, pointing up.
    forward(-letter_height/2)
    right(90)
    forward(letter_width)
    # Draw the right leg of the H.
    # The turtle starts at the right side of the bar, pointing right.
    left(90)
    forward(letter_height/2)
    forward(-letter_height)
    right(90)
    # The H is drawn.
    # The turtle is in the top right, pointing right.
    draw_space()


def draw_E():
    # Draw an E.
    left(90)
    forward(letter_height)
    right(90)
    forward(letter_width)
    forward(-letter_width)
    right(90)
    forward(letter_height/2)
    left(90)
    forward(letter_width)
    forward(-letter_width)
    right(90)
    forward(letter_height/2)
    left(90)
    forward(letter_width)
    draw_space()

def draw_L():
    # Draw an L
    left(90)
    forward(letter_height)
    forward(-letter_height)
    right(90)
    forward(letter_width)
    draw_space()

def draw_O():
    # Draw an O
    forward(letter_width)
    left(90)
    forward(letter_height)
    left(90)
    forward(letter_width)
    left(90)
    forward(letter_height)
    left(90)
    forward(letter_width)
    draw_space()

def draw_W():
    # This function will draw a W
    left(105)
    forward(letter_height)
    backward(letter_height)
    right(40)
    forward(letter_height/2)
    right(131)
    forward(letter_height/2)
    left(141)
    forward(letter_height)
    right(165)
    penup()
    forward(letter_height)
    left(90)
    draw_space()        

def draw_R(letter_width, letter_height):
    # This function will draw an R

    slant_height = (math.sqrt(letter_width**2 + (letter_height/2)**2))
    slant_angle = (90+(90-(math.degrees(math.acos(letter_width/slant_height)))))
    space_angle = (180 - slant_angle)

    left(90)
    forward(letter_height)
    right(90)
    forward(letter_width)
    right(90)
    forward(letter_height/2)
    right(90)
    forward(letter_width)
    left(slant_angle)
    forward(slant_height)
    left(space_angle)
    draw_space()

def draw_D(letter_width, letter_height):
    # This function will draw a REAL D

    angle_height = math.sqrt(letter_width**2 + (letter_height/2)**2)
    D_angle = (90+(math.degrees(math.acos(letter_width/angle_height))))
    Second_D_angle = ((90 - (D_angle-90)) + (90-(math.degrees(math.acos(letter_width/angle_height)))))
    D_space_angle = (math.degrees(math.atan(letter_width/(letter_height/2))))

    left(90)
    forward(letter_height)
    right(D_angle)
    forward(angle_height)
    right(Second_D_angle)
    forward(angle_height)
    left(90+D_space_angle)
    penup()
    forward(letter_width)
    draw_space()

下面是如何调用函数来绘制相应的字母:

onkey(draw_H, "h")
onkey(draw_E, "e")
onkey(draw_L, "l")
onkey(draw_O, "o")
onkey(draw_W, "w")
onkey(lambda: draw_R(letter_width, letter_height), "r")
onkey(lambda: draw_D(letter_width, letter_height), "d")

现在发生的事情是,当函数被调用并且用户重复按住 down/presses 相应函数的相同键时,会发生这种情况:

^这是我按住调用 H 键时发生的情况。还有:

^这就是我快速连续键入 R 和 H 时发生的情况。我希望在这些函数被调用和它们被激活之间有一个轻微的延迟,这样每个函数都能够完成它的过程,并且乌龟不会到处发疯。非常感谢有关此问题的任何帮助! :)

欢迎来到事件编程的世界!

让我们暂时忘记海龟吧。当某些字符在键盘上敲击时,您的程序会绘制您想要的东西。但是当你在画东西的时候,你不希望点击另一个角色开始新的绘图。所以你希望它不进入一个新的绘图程序,如果一个是活动的。你只需要一点同步。

由于所有发生在同一个线程中(无论如何,如果不同线程尝试访问屏幕,大多数 GUI 会表现得很糟糕),您实际上应该使用队列来保存绘图请求,如果您已经在绘图,则只需将请求排队:

q = Queue.Queue()
drawing = False
drawingLock = threading.Lock()

def draw(x):
    global drawing
    q.put(x)
    process = False:
    drawingLock.acquire()
    if not drawing:
        process = True
        drawing = True
    drawingLock.release()
    if process:
        while not q.empty():
            do_draw(q.get())  # call the actual drawings here
        drawingLock.acquire()
        drawing = False
        drawingLock.release()

应该从 onclick 事件调用此 draw 函数并将绘图分派给您的实际绘图函数。