python - turtle 图形按键事件不重复

标签 python events keyboard turtle-graphics

以下代码在不同环境中的行为有所不同。

在我的 Windows 计算机(Windows 10、Python 3.6)上,每次按键都会创建一个响应,然后什么也不会发生,直到我再次按键。

在“Python Trinkets”(https://trinket.io/python)上,我不需要重复按键,因为按住按键会创建重复事件。

我知道 Trinket 版本是 JS 实现,但这是否完全解释了其中的差异?该程序在 Linux 或操作系统上的行为是否相同?

更重要的是,有没有办法让它在 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()

最佳答案

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

按键重复是操作系统的一项功能,不同的系统对其处理方式有所不同。例如,我的 Apple OS X 系统上的按键默认重复,但我可以进入系统偏好设置/键盘并更改速率,甚至将其关闭(我这样做是为了测试下面的代码。)

如果您没有按键重复并且想要模拟它,这里有一个使用 turtle 计时器事件在按键按下时启动按键重复并在按键按下时将其关闭的简单示例:

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

但是,您的挑战是弄清楚何时需要这种模拟重复以及系统何时已经具有重复功能 - 同时激活这两种功能会很困惑。

关于python - turtle 图形按键事件不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44863600/

相关文章:

python - Pandas ._libs.hashtable.PyObjectHashTable.get_item KeyError : 0

python - 无法运行已安装的库

python - Travis CI 和 pytest 具有多个 python 版本

iphone - 在 iPad 上的 ModalView (FormSheet) 中隐藏键盘

c# - 从 Windows 窗体键中剥离修饰符

python - pandas 基于级别中项目数量的多索引切片

events - GWT 自定义事件

events - Vue.js 事件使用全局事件总线从子组件发射到(大)父组件

javascript - 无法删除 Javascript 中 event.data 附加的 jQuery 事件监听器

android - 我的应用程序的键盘永久可见