我想制作一个绘图应用程序,使用 WSAD 键移动海龟,Q 和 E 来升高和降低笔,我尝试添加一个对话框,以便您可以选择海龟的颜色,但是之后我改变了颜色,乌龟不会响应 WSAD 键绑定。有人能解释一下发生了什么以及如何解决这个问题吗?
代码如下:
import turtle
#WSAD to move
def up():
char.setheading(90)
char.forward(10)
def down():
char.setheading(270)
char.forward(10)
def goLeft():
char.setheading(180)
char.forward(10)
def goRight():
char.setheading(0)
char.forward(10)
#Raises and Lowers the pen
def lowerPen():
char.pendown()
def raisePen():
char.penup()
#change colors
def color():
data = turtle.simpledialog.askstring("Change Color", "Change Color to:")
char.color(data)
#Char info
char = turtle.Turtle()
char.speed(0)
screen = turtle.Screen()
screen.title("Arrow Paint")
#Make Turtle Listen to Keystrokes.
turtle.listen()
#Keybindings
turtle.onkey(color, "c")
turtle.onkey(up, "w")
turtle.onkey(down, "s")
turtle.onkey(goLeft, "a")
turtle.onkey(goRight, "d")
turtle.onkey(raisePen, "q")
turtle.onkey(lowerPen, "e")
turtle.done()
提前致谢
编辑:我也尝试用我找到的 turtle.simpledialog.askstring()
函数替换 textinput()
函数,但它仍然不会响应键绑定
答案 0 :(得分:0)
当你这样做时:
Cercle(int radius) {
this.radius = radius;
}
你让你的海龟窗口成为键盘事件的监听器。但是当你调用时:
turtle.listen()
或 data = turtle.simpledialog.askstring("Change Color", "Change Color to:")
,弹出的窗口成为键盘事件的监听器。因此,当您完成用户对话窗口时,请调用:
textinput()
之后再说。这应该允许您的海龟键盘事件像以前一样操作。修改后的代码:
turtle.listen()