乌龟图形按键事件不重复 [英] Turtle-graphics keypress event not repeating

查看:111
本文介绍了乌龟图形按键事件不重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在不同环境中的行为不同.

The following code behaves differently in different environments.

在我的Windows机器(Windows 10,Python 3.6)上,每次按键都会创建一个响应,然后直到再次按下某个键时,什么都不会发生.

On my Windows machine (Windows 10, Python 3.6), each keypress creates a response and then nothing happens until I press a key again.

在"Python小饰品"上( https://trinket.io/python ),我不需要重复按键,因为按住该键会产生重复事件.

On "Python Trinkets" (https://trinket.io/python) I don't need to repeat the keypress, as holding the key down creates repeat events.

我知道Trinket版本是JS实现,但这是否可以完全解释差异?该程序在Linux或OS上的行为是否相同?

I know that the Trinket version is a JS implementation, but does that explain the difference entirely? Would the program behave the same on Linux or OS?

更重要的是,是否有一种方法可以使它在Windows上运行,以便按住某个键可以创建重复事件?

More importantly, is there a way to make it work on Windows so that holding a key down creates repeat events?

# 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()

推荐答案

好,我找到了解决方案.

Ok, I've found the solution.

Trinket(JS)的实现不准确.

The Trinket (JS) implementation is inaccurate.

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

Using this code: wn.onkey(go_up, "Up") binds the function to the key-press event in "standard" Python, which means the function is only called once.

要在按住键的同时重复播放,需要以下内容:

To have it repeat while the key is held down, the following is needed:

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

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

这篇关于乌龟图形按键事件不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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