龟图形按键事件不重复

时间:2017-07-01 17:18:24

标签: python events keyboard turtle-graphics

以下代码在不同环境中的行为方式不同。

在我的Windows机器上(Windows 10,Python 3.6),每个按键都会创建一个响应,然后在我再按一次键之前没有任何反应。

On" Python Trinkets" (https://trinket.io/python)我不需要重复按键,因为按住键会产生重复事件。

我知道Trinket版本是一个JS实现,但这完全解释了它的区别吗?该程序在Linux或OS上的行为是否相同?

更重要的是,有没有办法让它在Windows上运行,以便按住键可以创建重复事件?

# import the turtle module so we can use all the neat code it contains
import turtle

# Create a variable `tina` that is a Turtle() object. Set shape to 'turtle'
tina = turtle.Turtle()
tina.shape('turtle')

# Create a variable `screen`, a Screen() object, that will handle keyss
screen = turtle.Screen()

# Define functions for each arrow key
def go_left():
  tina.left(7)

def go_right():
  tina.right(7)

def go_forward():
  tina.forward(10)

def go_backward():
  tina.backward(10)

# Tell the program which functions go with which keys
screen.onkey(go_left, 'Left')
screen.onkey(go_right, 'Right')
screen.onkey(go_forward, 'Up')
screen.onkey(go_backward, 'Down')

# Tell the screen to listen for key presses
screen.listen()
turtle.done()

2 个答案:

答案 0 :(得分:1)

好的,我找到了解决方案。

Trinket(JS)实现不准确。

使用此代码:wn.onkey(go_up, "Up")将函数绑定到“标准”Python中的按键事件,这意味着该函数只被调用一次。

要在按住键时重复此操作,需要执行以下操作:

def move(event):
    my_turtle.forward(5)

turtle.getcanvas().bind('<Up>', move)

答案 1 :(得分:0)

  

有没有办法让它在Windows上工作,以便持有一个密钥   down创建重复事件?

密钥重复是一种操作系统功能,不同的系统以不同的方式处理它。例如,我的Apple OS X系统上的按键默认重复,但我可以进入系统偏好设置/键盘并更改速率甚至关闭它(我测试下面的代码。)

如果你没有按键重复并想要模拟它,这里有一个简单的例子,说明使用乌龟计时器事件来启动按键重复按键并在按键时将其关闭:

# import the turtle module so we can use all the neat code it contains
from turtle import Turtle, Screen

KEY_REPEAT_RATE = 20  # in milliseconds

# Create a variable `tina` that is a Turtle() object. Set shape to 'turtle'
tina = Turtle('turtle')

# Create a variable `screen`, a Screen() object, that will handle keys
screen = Screen()

# Define functions for each arrow key
def go_left():
    tina.left(7)
    if repeating:
        screen.ontimer(go_left, KEY_REPEAT_RATE)

def go_right():
    tina.right(7)
    if repeating:
        screen.ontimer(go_right, KEY_REPEAT_RATE)

def go_forward():
    tina.forward(10)
    if repeating:
        screen.ontimer(go_forward, KEY_REPEAT_RATE)

def go_backward():
    tina.backward(10)
    if repeating:
        screen.ontimer(go_backward, KEY_REPEAT_RATE)

def start_repeat(func):
    global repeating

    repeating = True

    func()

def stop_repeat():
    global repeating

    repeating = False

repeating = False

# Tell the program which functions go with which keys
screen.onkeypress(lambda: start_repeat(go_left), 'Left')
screen.onkeyrelease(stop_repeat, 'Left')

screen.onkeypress(lambda: start_repeat(go_right), 'Right')
screen.onkeyrelease(stop_repeat, 'Right')

screen.onkeypress(lambda: start_repeat(go_forward), 'Up')
screen.onkeyrelease(stop_repeat, 'Up')

screen.onkeypress(lambda: start_repeat(go_backward), 'Down')
screen.onkeyrelease(stop_repeat, 'Down')

# Tell the screen to listen for key presses
screen.listen()

screen.mainloop()

然而,您面临的挑战是找出何时需要此模拟重复以及系统何时具有重复功能 - 两者都具有活动性将是混乱的。