I made a little python code that makes a ball bounce around the screen. Every time you left click it makes another ball. when you right click it clears the screen but you can't spawn more balls afterward... any help (python, tkinter)

from Tkinter import *
import time
import base_translate as code
info = {}
info["amount"] = code.code(11,62)

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        health = Label(master, text="")
        health.after(0, lambda:root.destroy())

root = Tk()

d = App(root)

info["x"] = root.winfo_screenwidth()-5
info["y"] = root.winfo_screenheight()-65
root.wait_window()

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        def deleteall(event):
            info["amount"] = code.code(11,62)
            frame.destroy()
            start()
            
        def makeball(event):
            Bounce(event.x_root, event.y_root,"right","down",info["amount"])
            info["amount"] = code.code(code.decode(info["amount"],62)+1,62)
            try:
                int(info["amount"])
            except ValueError:
                pass
            else:
                info["amount"] = code.code(code.decode(info["amount"],62)+10,62)

        def Bounce(x, y, movex, movey, ball):
            canvas.delete(ball)
            canvas.create_oval(x,y, x+25,y+25,fill = "black",outline="white",tag=ball)
            if movex == "right":
                x = x+1
                if x+25 >= info["x"]:
                    movex = "left"
            elif movex == "left":
                x = x-1
                if x <= 0:
                    movex = "right"
            if movey == "up":
                y = y-1
                if y <= 0:
                    movey = "down"
            elif movey == "down":
                y = y+1
                if y+25 >= info["y"]:
                    movey = "up"
            canvas.after(10, lambda:Bounce(x, y, movex, movey, ball))

        def start():
            frame = Frame(master)
            frame.pack()
            canvas = Canvas(frame, width = info["x"], height = info["y"], bg="White")
            canvas.pack(side=TOP)
            canvas.bind("<Button-1>", makeball)
            canvas.bind("<Button-3>", deleteall)
                
        canvas = Canvas(frame, width = info["x"], height = info["y"], bg="White")
        canvas.pack(side=TOP)
        canvas.after(100, lambda:Bounce(0,0,"right","down","a"))
        canvas.bind("<Button-1>", makeball)
        canvas.bind("<Button-3>", deleteall)

root = Tk()

d = App(root)

root.focus_force()
root.geometry('%dx%d+0+0'%(info["x"],info["y"]))
root.title("BALL!")
root.resizable(FALSE,FALSE)
root.wait_window()

I get the error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Python27\Thropian's games\bouncing ball.py", line 38, in makeball
Bounce(event.x_root, event.y_root,"right","down",info["amount"])
File "C:\Python27\Thropian's games\bouncing ball.py", line 48, in Bounce
canvas.delete(ball)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2223, in delete
self.tk.call((self._w, 'delete') + args)
TclError: invalid command name ".18725904.18725744"

Recommended Answers

All 4 Replies

You are obviously not passing the ball object to Bounce (Python naming conventions), so look at the places that call Bounce and see if the ball object is actually being passed.

You are obviously not passing the ball object to Bounce (Python naming conventions), so look at the places that call Bounce and see if the ball object is actually being passed.

I'm able to get new balls to spawn so the ball object is getting passed... until I destroy the frame and recreate it. I'm not sure what exactly is causing the problem

also I noticed I forgot my base_translate file so here it is

def code(number,base):
    if base < 2:
        base = 2
    if base > 62:
        base = 62
    numbers = [0,1,2,3,4,5,6,7,8,9,"a","b","c","d","e","f","g","h","i","j",
               "k","l","m","n","o","p","q","r","s","t","u","v","w","x","y",
               "z","A","B","C","D","E","F","G","H","I","J","K","L","M","N",
               "O","P","Q","R","S","T","U","V","W","X","Y","Z"]
    final = ""
    loc = 0
    try:
        int(number),int(base)
    except ValueError:
        raise ValueError('code(number,base): number and base must be in base10')
    else:
        number,base = int(number),int(base)
    while base**loc <= number:
        loc = loc + 1
    for x in range(loc-1,-1,-1):
        for y in range(base-1,-1,-1):
            if y*(base**x) <= number:
                final = "{}{}".format(final,numbers[y])
                number = number - y*(base**x)
                break
    return final

def decode(number,base):
    if base < 2:
        base = 2
    if base > 62:
        base = 62
    numbers = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f",
               "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v",
               "w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L",
               "M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
    final = 0
    loc = len(number)-1
    number = str(number)
    try:
        int(base)
    except ValueError:
        raise ValueError('decode(value,base): base must be in base10')
    for x in number:
        if numbers.index(x) > base:
            raise ValueError('{} is out of base{} range'.format(x,str(base)))
        final = final+(numbers.index(x)*(base**loc))
        loc = loc - 1
    return final

File "C:\Python27\Thropian's games\bouncing ball.py", line 48, in Bounce
canvas.delete(ball)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2223, in delete
self.tk.call((self._w, 'delete') + args)
TclError: invalid command name ".18725904.18725744"

Your error message says otherwise. The program is difficult to follow with functions that are not members of the class indented another level past __init__, which may be why there aren't many responses. Since you tag the canvas object with the contents of the variable "ball", it may be that the contents of the variable have changed since the first ball was created, or you are passing something other than the variable, ball, to the function. I'm not sure if using 2 classes with the same name, App, is harmful or not. Name mangling should keep them separate but it is bad practice. If you simplify and clean up the code a little and present a simple program that generates and then deletes a bouncing ball, there will likely be more people willing to help.

ok well I figured out a way to restart the whole program... which was good enough to prevent the error... I just needed to make a few variables but alls well that ends well

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.