我正在从一本书中输入一些代码(就像我们大多数人一样),但是当我运行它时,我收到了这个错误:NameError: name 'on_key_release' is not defined
。我输入完美,但它仍然无法正常工作,我知道我已经定义了它。可能是因为这本书适用于python3而且我有python2。完整的错误是:
Traceback (most recent call last):
File "C:\Users\caleb.sim\Downloads\Sim Inc\Sim Inc\Applications\Games\Ball and Bat\BallAndBat.py", line 97, in <module>
window.bind("<KeyRelease>", on_key_release)
NameError: name 'on_key_release' is not defined'
以下是代码:
import tkinter
import time
canvasWidth = 750
canvasHeight = 500
window = tkinter.Tk()
canvas = tkinter.Canvas(window, width = canvasWidth, height = canvasHeight)
canvas.pack()
bat = canvas.create_rectangle(0, 0, 40, 10, fill="dark turquoise")
ball = canvas.create_oval(20,0,30,10, fill="deep pink")
windowOpen = True
score = 0
bounceCount = 0
def main_loop():
while windowOpen == True:
move_bat()
move_ball()
window.update()
time.sleep(0.02)
if windowOpen == True:
check_game_over()
leftPressed = 0
rightPressed = 0
def on_key_press(event):
global leftPressed, rightPressed
if event.keysym == "Left":
leftPressed = 1
elif event.keysym == "Right":
rightPressed = 1
def on_key_press(event):
global leftPressed, rightPressed
if event.keysym == "Left":
leftPressed = 0
elif event.keysym == "Right":
rightPressed = 0
batSpeed = 6
def move_bat():
batMove = batSpeed * rightPressed - batSpeed * leftPressed
(batLeft, batTop, batRight, batBottom) = canvas.coords(bat)
if (batLeft > 0 or batMove > 0) and (batRight < canvasWidth or batMove < 0):
canvas.move(bat, batMove, 0)
ballMoveX = 4
ballMoveY = -4
setBatTop = canvasHeight - 40
setBatBottom = canvasHeight - 30
def move_ball():
global ballMoveX, ballMoveY, score, bounceCount, batSpeed
(ballLeft, ballTop, ballRight, ballBottom) = canvas.coords(ball)
if ballMoveX > 0 and ballRight > canvasWidth:
ballMoveX = -ballMoveX
if ballMoveX > 0 and ballLeft < 0:
ballMoveX = -ballMoveX
if ballMoveY > 0 and ballTop < 0:
ballMoveY = -ballMoveY
if ballMoveY > 0 and ballBottom > setBatTop and ballBottom < setBatBottom:
(batLeft, batTop, batRight, batBottom) = canvas.coords(bat)
if (ballMoveX > 0 and (ballRight + ballMoveX > batLeft and ballLeft < batRight) or ballMoveX < 0 and (ballRight > batLeft and ballLeft + ballMoveX < batRight)):
ballMoveY = -ballMoveY
score = score + 1
bounceCount = bounceCount + 1
if bounceCount == 4:
bounceCount = 0
batSpeed = batSpeed + 1
if ballMoveX > 0:
ballMoveX = ballMoveX + 1
else:
ballMoveX = ballMoveX - 1
ballMoveY = ballMoveY - 1
canvas.move(ball, ballMoveX, ballMoveY)
def check_game_over():
(ballLeft, ballTop, ballRight, ballBottom) = canvas.coords(ball)
if ballTop > canvasHeight:
print "Your Score Was: " + str(score)
playAgain = tkinter.messagebox.askyesno(message="Do you want to play again?")
if playAgain == True:
reset()
else:
close()
def close():
global windowOpen
windowOpen = False
window.destroy
def reset():
global score, bounceCount, batSpeed
global leftPressed, rightPressed
global ballMoveX, ballMoveY
leftPressed = 0
rightPressed = 0
ballMoveX = 4
ballMoveY = -4
canvas.coords(bat, 10, setBatTop, 50, setBatBottom)
canvas.coords(ball, 20, setBatTop-10, 30, setBatTop)
score = 0
bounceCount = 0
batSpeed = 6
window.protocol("WM_DELETE_WINDOW", close)
window.bind("<KeyPress>", on_key_press)
window.bind("<KeyRelease>", on_key_release)
reset()
main_loop()
<小时/> (格式化很痛苦!)
任何帮助将不胜感激!
-Caleb Sim
答案 0 :(得分:1)
你没有定义on_key_release。
在这里,我可以看到你定义了on_key_press两次。 你的意思是将其中一个定义为on_key_release?
def on_key_press(event):
global leftPressed, rightPressed
if event.keysym == "Left":
leftPressed = 1
elif event.keysym == "Right":
rightPressed = 1
def on_key_press(event):
global leftPressed, rightPressed
if event.keysym == "Left":
leftPressed = 0
elif event.keysym == "Right":
rightPressed = 0
答案 1 :(得分:1)
嗯,根据您问题中的代码判断,您尚未定义on_key_release
。您已经on_key_press
复制粘贴两次,因此其中一个可能会被更改。
另外:您可以通过突出显示代码并使用顶部栏上的{}按钮来格式化代码。