错误:无效的命令名“.4318456072”

2024-10-02 22:30:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用tkinter写一个snake的版本。我在尝试使用while True循环使画布小部件无限期移动时遇到了一个问题。代码似乎工作正常,但在关闭窗口时控制台中出现异常: Tkinter回调异常

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
    return self.func(*args)
  File "/Users/rachkids/Documents/Coding/Ball Game.py", line 33, in move_ball_right
    canvas.move(ball, xspeed, yspeed)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2416, in move
    self.tk.call((self._w, 'move') + args)
TclError: invalid command name ".4318456072"

下面是使对象移动的函数:

def move_ball_right(event):
    global spaceright
    global spaceleft
    if spaceright != 400:
        while True:
            xspeed = 10
            yspeed = 0
            canvas.move(ball, xspeed, yspeed)
            Tk.update(window1)
            spaceright = spaceright + 10
            spaceleft = spaceleft + 10
            time.sleep(0.25)

以下是我迄今为止的所有代码:

from Tkinter import *
from Tkinter import Canvas
import random
import time

window1 = Tk()
window1.title("Ball Game")
window1.config(background="black")
window1.geometry("400x400")
coord1 = 0
coord2 = 0
coord3 = 20
coord4 = 20
spaceright = 20
spaceleft = 400
spaceup = 400
spacedown = 20
random1 = random.choice([20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290,300,310,320,330,340,350,360,370,380])
random2 = random1 + 20


def generate_food():
    food = canvas.create_rectangle(random1, random1, random2, random2, fill="red")


def move_ball_right(event):
    global spaceright
    global spaceleft
    if spaceright != 400:
        while True:
            xspeed = 10
            yspeed = 0
            canvas.move(ball, xspeed, yspeed)
            Tk.update(window1)
            spaceright = spaceright + 10
            spaceleft = spaceleft + 10
            time.sleep(0.25)


def move_ball_left (event):
    global spaceright
    global spaceleft
    if spaceleft != 400:
        xspeed = -10
        yspeed = 0
        canvas.move(ball, xspeed, yspeed)
        Tk.update(window1)
        spaceleft = spaceleft - 10
        spaceright = spaceright - 10


def move_ball_down (event):
    global spacedown
    global spaceup
    if spacedown != 400:
        xspeed = 0
        yspeed = 10
        canvas.move(ball, xspeed, yspeed)
        Tk.update(window1)
        spacedown = spacedown + 10
        spaceup = spaceup + 10


def move_ball_up (event):
    global spacedown
    global spaceup
    if spaceup != 400:
        xspeed = 0
        yspeed = -10
        canvas.move(ball, xspeed, yspeed)
        Tk.update(window1)
        spaceup = spaceup - 10
        spacedown = spacedown - 10


canvas = Canvas(window1, width=400, height=400, bg="black", bd=0, highlightthickness=0, relief="ridge")
canvas.pack()
generate_food()
ball = canvas.create_rectangle(coord1, coord2, coord3, coord4, fill="white")
window1.bind('<Right>', move_ball_right)
window1.bind('<Left>', move_ball_left)
window1.bind('<Down>', move_ball_down)
window1.bind('<Up>', move_ball_up)
window1.mainloop()

任何帮助都将不胜感激!你知道吗


Tags: eventmovetkinterdefglobaltkcanvasball
1条回答
网友
1楼 · 发布于 2024-10-02 22:30:48

这是因为当你杀死tkinter窗口时,画布对象也会被销毁,它是tkinter窗口的一个子对象(在这个例子中,画布被报告为一个对象编号4318456072)。但是while循环仍然在运行,即使tkinter窗口不见了。但是canvas对象不存在,这会产生错误。你知道吗

更正可以包括在画布.移动语句,并在发生此异常时导致while循环中断。你知道吗

特别是,应该捕获while循环中与tkinter有关的任何内容。这里非常简单的错误检查可能是:

try:
    canvas.move(ball, xspeed, yspeed)
    Tk.update(window1)
except:
    break

相关问题 更多 >