Home > other >  why am i getting a trace back in my code for my move ball command as it is stopping the rest of my c
why am i getting a trace back in my code for my move ball command as it is stopping the rest of my c

Time:06-14

i am making a pong game

i managed to code the ball to bounce around the window but it seems that the code, for using user input to get the rectangles to move isn't working. when i run the code i have been getting a traceback to my move.ball command relating to making the ball bounce, and i believe that may be stopping the rest of the code from running. I have included the full code in case any errors can be spotted in it.

from tkinter import *
import tkinter as tkr
import time
tk = tkr.Tk()
Canvas = tkr.Canvas(tk, width=300, height=400)
Canvas.grid()
ball = Canvas.create_oval(3,3,40,40,fill="light blue")
player1 = Canvas.create_rectangle(20,5,90,30,fill="black")
Canvas.moveto(player1, 120, 380)
player2 = Canvas.create_rectangle(20,5,90,30,fill="black")
Canvas.moveto(player2, 120, -5)
x = 1
y = 3
while True:
    Canvas.move(ball,x,y)
    pos = Canvas.coords(ball)
if pos[3] >= 400 or pos[1] <= 0:
    y = -y
if pos[2] >= 300 or pos[0] <= 0:
    x = -x
tk.update()
time.sleep(0.025)
pass

tk.mainloop()

def left(event):
    x == -10
    y == 0
Canvas.move(player1, x, y)

def right(event):
    x == 10
    y == 0
Canvas.move(player1, x, y)

def up(event):
    x == 0
    y == -10
Canvas.move(player1, x, y)

def down(event):
    x == -10
    y == 0
Canvas.move(player1, x, y)

root.bind("<Left>", left)
root.bind("<Right>", right)
root.bind("<Up>", up)
root.bind("<Down>", down)



tk.mainloop()

i am getting this trace back-

Traceback (most recent call last):
  File "C:/Users/amarb/Desktop/code/pong.py", line 15, in <module>
    Canvas.move(ball,x,y)
  File "C:\Users\amarb\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2949, in move
    self.tk.call((self._w, 'move')   args)
_tkinter.TclError: invalid command name ".!canvas"

CodePudding user response:

.!canvas is the internal identifier of the canvas widget that you created. This error is happening because you try to run code after mainloop() returns, which is after the window being destroyed. You can't interact with widgets after they have been destroyed.

CodePudding user response:

Your pos, update and time should be inside while block condition. Btw, you will have to fix left, right up and down. Also you can't have 2 mainloop()

from tkinter import *
import tkinter as tkr
import time


tk = tkr.Tk()
Canvas = tkr.Canvas(tk, width=300, height=400)
Canvas.grid()

ball = Canvas.create_oval(3,3,40,40,fill="light blue")
player1 = Canvas.create_rectangle(20,5,90,30,fill="black")
Canvas.moveto(player1, 120, 380)
player2 = Canvas.create_rectangle(20,5,90,30,fill="black")
Canvas.moveto(player2, 120, -5)

x = 1
y = 3

while True:
    Canvas.move(ball,x,y)
    pos = Canvas.coords(ball)
    if pos[3] >= 400 or pos[1] <= 0:
        y = -y
    if pos[2] >= 300 or pos[0] <= 0:
        x = -x
    tk.update()
    time.sleep(0.025)
    #pass
 

def left(event):
    x == -10
    y == 0
Canvas.move(player1, x, y)

def right(event):
    x == 10
    y == 0
Canvas.move(player1, x, y)


def up(event):
    x == 0
    y == -10
Canvas.move(player1, x, y)

def down(event):
    x == -10
    y == 0
Canvas.move(player1, x, y)


root.bind("<Left>", left)
root.bind("<Right>", right)
root.bind("<Up>", up)
root.bind("<Down>", down)

tk.mainloop()

Output:

enter image description here

  • Related