1

I made an app which works very well and smoothly, but when I stop the code it gives me an error like this:

Traceback (most recent call last):
File "/Users/Andrey/Desktop/Python/BubleBlasterGame.py", line 131, in <module>
show_score(score)
File "/Users/Andrey/Desktop/Python/BubleBlasterGame.py", line 91, in show_score
c.itemconfig(score_text, text=str(score))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 2417, in itemconfigure
return self._configure(('itemconfigure', tagOrId), cnf, kw)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1321, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".4387063736"
>>> 

And this function is in a loop soo... This is my code (please do not copy):

while True:
from tkinter import *
from random import randint
from math import sqrt
from time import sleep, time
#Game Interface:
#The main play ground:
HEIGHT = 500
WIDTH = 800
window = Tk ()
window.title('BubleBlaster')
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='darkblue')
c.pack()
#The ship:
ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
ship_id2 = c.create_oval(0, 0, 30, 30, outline='red')
#Radius of the ship:
SHIP_R = 15
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
c.move(ship_id, MID_X, MID_Y)
c.move(ship_id2, MID_X, MID_Y)
#Controls:
SHIP_SPD = 50
def move_ship(event):
    if event.keysym == 'Up':
        c.move(ship_id, 0, -SHIP_SPD)
        c.move(ship_id2, 0, -SHIP_SPD)
    elif event.keysym == 'Down':
        c.move(ship_id, 0, SHIP_SPD)
        c.move(ship_id2, 0, SHIP_SPD)
    elif event.keysym == 'Left':
        c.move(ship_id, -SHIP_SPD, 0)
        c.move(ship_id2, -SHIP_SPD, 0)
    elif event.keysym == 'Right':
        c.move(ship_id, SHIP_SPD, 0)
        c.move(ship_id2, SHIP_SPD, 0)
c.bind_all('<Key>', move_ship)
#Bubles:
bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 10
GAP = 100
def create_buble():
    x = WIDTH + GAP
    y = randint(0, HEIGHT)
    r = randint(MIN_BUB_R, MAX_BUB_R)
    id1 = c.create_oval(x - r, y - r, x + r, y + r, outline='white')
    bub_id.append(id1)
    bub_r.append(r)
    bub_speed.append(randint(1, MAX_BUB_SPD))
#Bubles movement:
def move_bubles():
    for i in range (len(bub_id)):
        c.move(bub_id[i], -bub_speed[i], 0)
def get_coords(id_num):
    pos = c.coords(id_num)
    x = (pos[0] + pos[2])/2
    y = (pos[1] + pos[3])/2
    return x, y
def del_buble(i):
    del bub_r[i]
    del bub_speed[i]
    c.delete(bub_id[i])
    del bub_id[i]
def clean_up_bubs():
    for i in range (len(bub_id) -1, -1, -1):
        x, y = get_coords (bub_id[i])
        if x < -GAP:
            del_buble(i)
def distance (id1, id2):
    x1, y1 = get_coords(id1)
    x2, y2 = get_coords(id2)
    return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def collision():
    points = 0
    for bub in range (len(bub_id)-1, -1, -1):
        if distance (ship_id2, bub_id[bub]) < (SHIP_R + bub_r[bub]):
            points += (bub_r[bub] + bub_speed[bub] - 20)
            del_buble(bub)
    return points
c.create_text(50, 30, text='TIME', fill='white' )
c.create_text(150, 30, text='SCORE', fill='white' )
time_text = c.create_text(50, 50, fill='white' )
score_text = c.create_text(150, 50, fill='white' )
def show_score(score):
    c.itemconfig(score_text, text=str(score))
def show_time(time_left):
    c.itemconfig(time_text, text=str(time_left))
BUB_CHANCE = 10
TIME_LIMIT = 30
BONUS_SCORE = 500
score = 0
bonus = 0
end = time() + TIME_LIMIT
#INtro
main_intro = c.create_text(MID_X, MID_Y, fill='white', font=('Helvetica', 30), text='INTRO')
intro = c.create_text(MID_X, MID_Y + 30, fill='white', text='Get ready! Starting in 5 sec!')
intro2 = c.create_text(MID_X, MID_Y + 45, fill='white', text='To move press the arrows!')
#intro3 = c.create_text(MID_X, MID_Y + 60, fill='white', text='To slow down press s!')
#intro4 = c.create_text(MID_X, MID_Y + 75, fill='white', text='To speed up press f!')
window.update()
sleep(5)
c.itemconfig(main_intro, state='hidden')
c.itemconfig(intro, state='hidden')
c.itemconfig(intro2, state='hidden')
#c.itemconfig(intro3, state='hidden')
#c.itemconfig(intro4, state='hidden')
#def slower(event):
    #global SHIP_SPD
    #SHIP_SPD = SHIP_SPD - 5
#def faster(event):
    #global SHIP_SPD
    #SHIP_SPD = SHIP_SPD + 5
#c.bind_all('<KeyPress-s>', slower)
#c.bind_all('<KeyPress-f>', faster)
#main game loop
while time() < end:
    if randint(1, BUB_CHANCE) == 1:
        create_buble()
    move_bubles()
    clean_up_bubs()
    score += collision()
    if (int(score / BONUS_SCORE)) > bonus:
        bonus += 1
        end += TIME_LIMIT
    show_score(score)
    show_time(int(end - time()))
    window.update()
    sleep(0.01)
c.create_text(MID_X, MID_Y, \
              text='GAME OVER', fill='white', font=('Helvetica',30))
c.create_text(MID_X, MID_Y + 30, \
              text='Score: '+ str(score), fill='white')
c.create_text(MID_X, MID_Y + 45, \
              text='Bonus time: '+ str(bonus*TIME_LIMIT), fill='white')
window.update()
restarting_in = c.create_text(MID_X, MID_Y + 60, fill='white', text='Restarting in 10 seconds, get ready!')
window.update()
sleep(10)
window.destroy()

Also there are spaces because of the while true and stuff so don't look at that. Any help appreciated, sorry I don't really know how to do the code part in StackOverFlow.

st4tic
  • 369
  • 2
  • 3
  • 8
  • 1
    We need to see more of your code, preferably a [mcve] that focuses on your issue. But that error message commonly happens when your code attempt to access a widget that has been destroyed. Take a look at http://stackoverflow.com/questions/22947911/python-tk-tkinter-tclerror-invalid-command-name-42818376 – PM 2Ring Oct 16 '16 at 12:12
  • add `print(c, i, bub_id[i])` to see which element makes problem. And maybe show how you create `bub_id` and add elements. – furas Oct 16 '16 at 16:10
  • is `move_bubles` called via `after`? – Bryan Oakley Oct 17 '16 at 15:12
  • Also the Bub_id and the bub_speed are lists soo... – st4tic Oct 17 '16 at 16:33
  • We still don't have enough information to solve your problem. We really need a [MCVE](http://stackoverflow.com/help/mcve). However, you shouldn't use `time.sleep` in Tkinter programs as that puts everything to sleep which mean Tkinter can't respond to GUI events. Take a look at the `.after` method, as shown in [this answer](http://stackoverflow.com/a/19887761/4014959). – PM 2Ring Oct 18 '16 at 07:11
  • It seems like it tries to call a deleted variable or list or whatever when I close the gui (stop the code), so I need to have some kind of on-stop function which is going to immediately break the code but there is no such thing so... – st4tic Oct 19 '16 at 13:01

0 Answers0