【问题标题】:How to fix: NameError: name 'on_key_release' is not defined? (Python)如何修复:NameError:未定义名称'on_key_release'? (Python)
【发布时间】:2017-11-08 04:23:45
【问题描述】:

我正在从书中输入一些代码(就像我们大多数人一样),但是当我运行它时,我收到了这个错误: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()


(格式化很痛苦!)

任何帮助将不胜感激!

-卡莱布·辛

【问题讨论】:

  • 错误告诉你到底哪里出了问题。

标签: python python-2.7 tkinter


【解决方案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

【讨论】:

    【解决方案2】:

    好吧,从您问题中的代码来看,您还没有定义on_key_release。您已将 on_key_press 复制粘贴了两次,因此其中一个可能需要更改。

    另外:您可以通过突出显示代码并使用顶部栏上的 {} 按钮来格式化代码。

    【讨论】:

      猜你喜欢
      • 2021-01-24
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多