Python 按键属性错误
Python attribute error onkeypress
我是 Python 的新手。我在 raspberry pi 上使用 IDLE(使用 python 2.7)。我一直无法从我的教程(猫捉老鼠游戏)中编译最新程序。我收到以下错误:
Traceback (most recent call last) :
File "/home/pi/pyth-prog/Python_Cat_and-mouse.py", line 47, in <module> window.onkeypress(up, "Up")
AttributeError: '__Screen' object has no attribute 'onkeypress'
我的代码如下所示:
import turtle
import time
boxsize =200
caught= False
score= 0
#function that are called keypresses
def up():
mouse.forward(10)
checkbound()
def left():
mouse.left(45)
def right():
mouse.right(45)
def back():
mouse.back(10)
def quitTurtles():
window.bye()
#stop the ;ouse fro; leaving the square set by box sizes
def checkbound():
global boxsize
if mouse.xcor() > boxsize:
mouse.goto(boxsize, mouse.ycor())
if mouse.xcor() < -boxsize:
mouse.goto(-boxsize, mouse.ycor())
if mouse.ycor() > boxsize:
mouse.goto(mouse.xcor(), boxsize)
if mouse.ycor < -boxsize:
mouse.goto(mouse.xcor(), -boxsize)
#Set up screen
window=turtle.Screen()
mouse=turtle.Turtle()
cat=turtle.Turtle()
mouse.penup()
mouse.penup()
mouse.goto(100, 100)
#add key listeners
window.onkeypress(up, "Up")
window.onkeypress(left, "Left")
window.onkeypress(right, "Right")
window.onkeypress(back, "Down")
window.onkeypress(quitTurtles, "Escape")
difficulty=window.numinput("Difficulty", "Enter a difficulty from easy (1), for hard (5) ", minval=1, maxval=5)
window.listen()
#main loop
#note how it changes with difficulty
while not caught:
cat.setheading(cat.towards(mouse))
cat.forward(8+difficulty)
score=score+1
if cat.distance(mouse) < 5:
caught=True
time.sleep(0.2-(0.01*difficulty))
window.textinput("GAME OVER", "Well done. You scored:"+ str(score*difficulty))
window.bye()
I use IDLE(Using python 2.7) on my raspberry pi
Python 2.7 的 turtle.py 仅定义 onkey()
-- onkeypress()
变体已添加到 Python 3(与 onkey()
称为 onkeyrelease()
)
简短回答,尝试将 onkeypress()
更改为 onkey()
。
一旦你越过这个障碍,numinput()
和 textinput()
也是 Python 3:
difficulty=window.numinput("Difficulty", "Enter a difficulty from easy (1), for hard (5) ", minval=1, maxval=5)
...
window.textinput("GAME OVER", "Well done. You scored:"+ str(score*difficulty))
所以他们可能也需要处理。
基于 turtle
来自 Python 3.5.
不需要window.
但必须在turtle.Screen()
之后执行
import turtle
# --- based on turtle in Python 3.5 ---
import tkSimpleDialog
def numinput(title, prompt, default=None, minval=None, maxval=None):
return tkSimpleDialog.askfloat(title, prompt, initialvalue=default,
minvalue=minval, maxvalue=maxval)
def textinput(title, prompt):
return tkSimpleDialog.askstring(title, prompt)
# --- main ---
window = turtle.Screen()
# doesn't need `window.` but has to be executed after `turtle.Screen()`
difficulty = numinput("Difficulty", "Enter a difficulty from easy (1), for hard (5) ", minval=1, maxval=5)
textinput("GAME OVER", "Well done. You scored:" + str(0))
我是 Python 的新手。我在 raspberry pi 上使用 IDLE(使用 python 2.7)。我一直无法从我的教程(猫捉老鼠游戏)中编译最新程序。我收到以下错误:
Traceback (most recent call last) :
File "/home/pi/pyth-prog/Python_Cat_and-mouse.py", line 47, in <module> window.onkeypress(up, "Up")
AttributeError: '__Screen' object has no attribute 'onkeypress'
我的代码如下所示:
import turtle
import time
boxsize =200
caught= False
score= 0
#function that are called keypresses
def up():
mouse.forward(10)
checkbound()
def left():
mouse.left(45)
def right():
mouse.right(45)
def back():
mouse.back(10)
def quitTurtles():
window.bye()
#stop the ;ouse fro; leaving the square set by box sizes
def checkbound():
global boxsize
if mouse.xcor() > boxsize:
mouse.goto(boxsize, mouse.ycor())
if mouse.xcor() < -boxsize:
mouse.goto(-boxsize, mouse.ycor())
if mouse.ycor() > boxsize:
mouse.goto(mouse.xcor(), boxsize)
if mouse.ycor < -boxsize:
mouse.goto(mouse.xcor(), -boxsize)
#Set up screen
window=turtle.Screen()
mouse=turtle.Turtle()
cat=turtle.Turtle()
mouse.penup()
mouse.penup()
mouse.goto(100, 100)
#add key listeners
window.onkeypress(up, "Up")
window.onkeypress(left, "Left")
window.onkeypress(right, "Right")
window.onkeypress(back, "Down")
window.onkeypress(quitTurtles, "Escape")
difficulty=window.numinput("Difficulty", "Enter a difficulty from easy (1), for hard (5) ", minval=1, maxval=5)
window.listen()
#main loop
#note how it changes with difficulty
while not caught:
cat.setheading(cat.towards(mouse))
cat.forward(8+difficulty)
score=score+1
if cat.distance(mouse) < 5:
caught=True
time.sleep(0.2-(0.01*difficulty))
window.textinput("GAME OVER", "Well done. You scored:"+ str(score*difficulty))
window.bye()
I use IDLE(Using python 2.7) on my raspberry pi
Python 2.7 的 turtle.py 仅定义 onkey()
-- onkeypress()
变体已添加到 Python 3(与 onkey()
称为 onkeyrelease()
)
简短回答,尝试将 onkeypress()
更改为 onkey()
。
一旦你越过这个障碍,numinput()
和 textinput()
也是 Python 3:
difficulty=window.numinput("Difficulty", "Enter a difficulty from easy (1), for hard (5) ", minval=1, maxval=5)
...
window.textinput("GAME OVER", "Well done. You scored:"+ str(score*difficulty))
所以他们可能也需要处理。
基于 turtle
来自 Python 3.5.
不需要window.
但必须在turtle.Screen()
之后执行
import turtle
# --- based on turtle in Python 3.5 ---
import tkSimpleDialog
def numinput(title, prompt, default=None, minval=None, maxval=None):
return tkSimpleDialog.askfloat(title, prompt, initialvalue=default,
minvalue=minval, maxvalue=maxval)
def textinput(title, prompt):
return tkSimpleDialog.askstring(title, prompt)
# --- main ---
window = turtle.Screen()
# doesn't need `window.` but has to be executed after `turtle.Screen()`
difficulty = numinput("Difficulty", "Enter a difficulty from easy (1), for hard (5) ", minval=1, maxval=5)
textinput("GAME OVER", "Well done. You scored:" + str(0))