Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[error]Traceback
#1

# made by kikoko_2

from tkinter import *
GARUMS = 500
PLATUMS = 800
logs = Tk()
logs.title('Burbuļu spridzinātājs')
a = Canvas(logs, width=PLATUMS, height=GARUMS, bg='darkblue')
a.pack()

kuģa_id = a.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
kuģa_id2 = a.create_oval(0, 0, 30, 30, outline='red')
KUĢA_R = 15
VID_X = PLATUMS / 2
VID_Y = GARUMS / 2
a.move(kuģa_id, VID_X, VID_Y)
a.move(kuģa_id2, VID_X, VID_Y)

KUĢA_ĀTR = 10
def pārvietot_kuģi(notikums):
    if notikums.keysym == 'Up':
        a.move(kuģa_id, 0, -KUĢA_ĀTR)
        a.move(kuģa_id2, 0, -KUĢA_ĀTR)
    elif notikums.keysym == 'Down':
        a.move(kuģa_id, 0, KUĢA_ĀTR)
        a.move(kuģa_id2, 0, KUĢA_ĀTR)
    elif notikums.keysym == 'Left':
        a.move(kuģa_id, -KUĢA_ĀTR, 0)
        a.move(kuģa_id2, -KUĢA_ĀTR, 0)
    elif notikums.keysym == 'Right':
        a.move(kuģa_id, KUĢA_ĀTR, 0)
        a.move(kuģa_id2, KUĢA_ĀTR, 0)
a.bind_all('<Key>', pārvietot_kuģi)

from random import randint
burb_id = list()
burb_r = list()
burb_ātrums = list()
MIN_BURB_R = 10
MAX_BURB_R = 30
MAX_BURB_ĀTR = 10
ATSTARPE = 100
def izveidot_burbuli():
    x = PLATUMS + ATSTARPE
    y = randint(0, GARUMS)
    r = randint(MIN_BURB_R, MAX_BURB_R)
    id1 = a.create_oval(x - r, y - r, x + r, y + r, outline='white')
    burb_id.append(id1)
    burb_r.append(r)
    burb_ātrums.append(randint(1, MAX_BURB_ĀTR))

def pārvietot_burbuļus():
    for i in range(len(burb_id)):
        a.move(burb_id[i], -burb_ātrums[i], 0)

def iegūt_koord(id_skaitlis):
    poz = a.coords(id_skaitlis)
    x = (poz[0] + poz[2])/2
    y = (poz[1] + poz[3])/2
    return x, y

def dzēst_burbuli(i):
    del burb_r[i]
    del burb_ātrums[i]
    a.delete(burb_id[i])
    del burb_id[i]

def notīrīt_burbuļus():
    for i in range(len(burb_id)-1, -1, -1):
        x, y = iegūt_koord(burb_id[i])
        if x < -ATSTARPE:
            dzēst_burbuli(i)

from math import sqrt
def attālums(id1, id2):
    x1, y1 = iegūt_koord(id1)
    x2, y2 = iegūt_koord(id2)
    return sqrt((x2 - x1)**2 + (y2 - y1)**2)

def sadursme():
    punkti = 0
    for burb in range(len(burb_id)-1, -1, -1):
        if attālums(kuģa_id2, burb_id[burb]) < (KUĢA_R + burb_r[burb]):
            punkti += (burb_r[burb] + burb_ātrums[burb])
            dzēst_burbuli(burb)
    return punkti

a.create_text(50, 30, text='TIME', fill='white')
a.create_text(150, 30, text='SCORE', fill='white')
laiks_teksts = a.create_text(50, 50, fill='white')
rezultāts_teksts = a.create_text(150, 50, fill='white')
def parādīt_rezultātu(rezultāts):
    a.itemconfig(rezultāts_teksts, text=str(rezultāts))
def parādīt_laiku(laiks_palicis):
    a.itemconfig(laiks_teksts, text=str(laiks_palicis))

from time import sleep, time
BURB_NEJAUŠI = 10
LAIKA_IEROBEŽOJUMS = 30
PAPILDLAIKA_REZ = 1000
rezultāts = 0
papildu = 0
beigas = time() + LAIKA_IEROBEŽOJUMS

rezultāts = 0
#SPĒLES GALVENAIS CIKLS
while time() < beigas:
    if randint(1, BURB_NEJAUŠI) == 1:
        izveidot_burbuli()
        pārvietot_burbuļus()
        notīrīt_burbuļus()
        rezultāts += sadursme()
        if (int(rezultāts / PAPILDLAIKA_REZ)) > papildu:
            papildu += 1
            beigas += LAIKA_IEROBEŽOJUMS
        parādīt_rezultātu(rezultāts)
        parādīt_laiku(int(beigas - time()))
        logs.update()
        sleep(0.01)

a.create_text(VID_X, VID_Y, \
    text='GAME OVER', fill='white', font=('Helvetica',30))
a.create_text(VID_X, VID_Y + 30, \
    text='Score: '+ str(rezultāts), fill='white')
a.create_text(VID_X, VID_Y + 45, \
    text='Extra time: '+ str(papildu*LAIKA_IEROBEŽOJUMS), fill='white')
Error:
Traceback (most recent call last): File "C:\Users\test\Desktop\python game\myfirstgame.py", line 109, in <module> izveidot_burbuli() File "C:\Users\test\Desktop\python game\myfirstgame.py", line 47, in izveidot_burbuli id1 = a.create_oval(x - r, y - r, x + r, y + r, outline='white') File "C:\Users\test\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2489, in create_oval return self._create('oval', args, kw) File "C:\Users\test\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2474, in _create *(args + self._options(cnf, kw)))) _tkinter.TclError: invalid command name ".!canvas"
Hello, I don't understand what I did wrong in the code. IDLE wasn't even responding for a while. Think
Hopefully someone finds the error. Pray
Thanks!
Reply
#2
Try using global in function. See if that fixes the problem.
def somefunction():
    global a

Never Mine. It happening when you close game. It making a call after you destroy canvas.

Don't use time.sleep.
tkinter has there own. Every widget has .after.
def loop():
    # roughly 30 frames per second
    a.after(1000/30, loop) # timer_id = a.after(1000/30, loop, loop args)
Sample here
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Traceback Error mg24 2 954 Nov-21-2022, 02:35 PM
Last Post: snippsat
  Traceback error in PyCharm but not in other IDEs? devansing 7 6,384 Mar-05-2020, 11:27 AM
Last Post: buran
  Nameparser - Traceback error tjnichols 2 2,386 Feb-28-2020, 05:11 PM
Last Post: GodMaster
  I have Traceback error and a type error and i dont know how to fix it coltron1282 2 2,533 Feb-07-2019, 03:15 PM
Last Post: coltron1282
  Unexpected output, TypeError and traceback error fier259 2 3,122 May-06-2018, 10:52 PM
Last Post: fier259
  CSV unable to import (traceback error) learnermarx 3 6,342 Feb-09-2018, 03:24 PM
Last Post: buran
  Python Error (Traceback & ValueError) Help! helpplease 4 5,980 Dec-19-2016, 06:07 PM
Last Post: Ofnuts
  Python Traceback Error CaiGengYang 4 7,902 Nov-28-2016, 08:51 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020