海龟图形:如何实现暂停功能? [英] turtle graphics: How do I implement a pause function?

查看:81
本文介绍了海龟图形:如何实现暂停功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 3 海龟图形来做一些类似演示软件的事情:画一些东西,暂停按键以便演示者可以解释,然后绘制下一个东西.

I'm trying to use python 3 turtle graphics to do something like presentation software: draw something, pause for a keystroke so the presenter can explain, then draw the next thing.

这是我尝试过的一种解决方案(不起作用):

Here is one solution I've tried (that doesn't work):

import turtle
import time

paused = False

def unpause():
    print("unpause() called")
    global paused
    paused = False

def pause():
    global paused
    paused = True
    while paused:
        time.sleep(0.1)


t = turtle.Turtle()

# set up listener
t.screen.listen()
t.screen.onkeypress(unpause)

# draw something
t.hideturtle()
t.pensize(5)
t.speed(1)
t.pu()
t.goto(-300,-300)
t.pd()
t.goto(-300, 300)

# pause until key is pressed
pause()

# draw some more
t.pu()
t.goto(300,-300)
t.pd()
t.goto(300, 300)

t.screen.mainloop()

问题是睡眠调用循环完全阻止了按键被检测到,即使我使用很短(100 毫秒)睡眠的 while 循环也是如此.

The problem is that the sleep call loop totally blocks the keypress from being detected, even when I use a while loop of very short (100ms) sleeps.

如果我在绘制第一行时按下一个键,我会在控制台中看到unpause() 调用",所以我知道键绑定处于活动状态.

If I hit a key while the first line is drawing, I see "unpause() called" in my console, so I know that the key binding is active.

为什么没有检测到按键?我不知道内部结构,但我认为击键会记录在某个缓冲区中,并且在睡眠调用之间的休息期间,侦听器将读取缓冲区并取消设置 paused 全局变量.这不会发生.

Why doesn't the keypress get detected? I don't know about the internals, but I thought that the keystroke would be recorded in a buffer somewhere, and during the break between sleep calls, the listener would read the buffer and unset the paused global variable. This is not happening.

还有其他方法可以实现吗?

Is there some other way I could implement this?

这是在 Debian Linux 系统上.

This is on a Debian Linux system.

推荐答案

根据您的建议给我的想法(感谢 martineau 和 kederrac!)我想出了一个解决方案.它涉及将我的每个绘图任务包装在一个函数中,然后使用一个调度函数,该函数要么等待带有 ontimer 循环的按键,要么调用下一个绘图函数.

Taking the ideas your suggestions have given me (thanks martineau and kederrac!) I was able to come up with a solution. It involves wrapping each of my drawing tasks in a function, then using a dispatch function that either waits for a keypress with an ontimer loop, or calls the next drawing function.

这个概念验证代码使用了太多的全局变量,但它展示了技术:

This proof-of-concept code uses entirely too many globals, but it shows the technique:

import turtle

t = turtle.Turtle()
paused = False
current_task = 0

def unpause():
    global paused
    paused = False

def turtle_setup():
    global t
    t.screen.onkeypress(unpause)
    t.screen.listen()
    t.hideturtle()
    t.pensize(5)
    t.speed(1)

def draw_task_finished():
    global paused, current_task, drawing_tasks
    current_task += 1
    paused = True
    if current_task < len(drawing_tasks):
        draw_task_after_keypress()

def draw_task_after_keypress():
    global paused, current_task
    if paused:
        turtle.ontimer(draw_task_after_keypress, 100)
    else:
        drawing_tasks[current_task]()

def draw_thing_one():
    global t
    t.pu()
    t.goto(-300,-300)
    t.pd()
    t.goto(-300, 300)
    draw_task_finished()

def draw_thing_two():
    global t
    t.pu()
    t.goto(300,-300)
    t.pd()
    t.goto(300, 300)
    draw_task_finished()

drawing_tasks = [draw_thing_one, draw_thing_two]

turtle_setup()
drawing_tasks[0]()

t.screen.mainloop()

这篇关于海龟图形:如何实现暂停功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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