Python Turtle图形键盘命令 [英] Python Turtle Graphics Keyboard Commands

查看:309
本文介绍了Python Turtle图形键盘命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人对使用键盘命令在python 2.7中控制乌龟图形有任何见识吗?我在这个网站和其他网站上进行了广泛的研究,觉得我做对了,但是它不想为我工作.以下是我到目前为止的情况,谁能告诉我我要去哪里错了????

Anybody have any insight into controlling turtle graphics in python 2.7 with keyboard commands? I have done extensive research on this website and others and feel like I am doing the right thing but it just doesn't want to work for me. Below is what I have so far, can anyone tell me where I am going wrong????

from turtle import *
turtle.setup(500, 500)
wn = turtle.Screen()
wn.title("Turtle Keys")
move = turtle.Turtle()
showturtle()

def k1():
move.forward(45)

def k2():
move.left(45)

def k3():
move.right(45)

def k4():
move.back(45)

wn.onkey(k1, "Up")
wn.onkey(k2, "Left")
wn.onkey(k3, "Right")
wn.onkey(k4, "Down")

wn.listen()

推荐答案

当您指定import *时,您不必使用turtle.,还必须使用mainloop()read(无限循环)来监视用户交互,在您的示例中wn也是不必要的.

When you specified import * you don't have to use turtle., also you have to use mainloop() read (infinite loop) to watch over user interactions, in your example wn is also unnecessary.

这是工作代码...

from turtle import *
setup(500, 500)
Screen()
title("Turtle Keys")
move = Turtle()
showturtle()

def k1():
    move.forward(45)

def k2():
    move.left(45)

def k3():
    move.right(45)

def k4():
    move.back(45)

onkey(k1, "Up")
onkey(k2, "Left")
onkey(k3, "Right")
onkey(k4, "Down")

listen()
mainloop()

这篇关于Python Turtle图形键盘命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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