Python: Tkinter TclError: can't invoke "image" command Python: Tkinter TclError: can't invoke "image" command tkinter tkinter

Python: Tkinter TclError: can't invoke "image" command


You have a few places in your code where you forgot self. But the cause of your main error is that you need to pass the image name as a keyword arg. Here's a repaired version of your code.

from tkinter import *import timefrom PIL import Image,ImageFilterfrom PIL import ImageTkclass alien(object):     def __init__(self):        self.root = Tk()        self.canvas = Canvas(self.root, width=400, height=400)        self.canvas.pack()        self.cardPath = Image.open('bubble.png')        self.resCard = self.cardPath.resize((100, 100))        self.CardVar = ImageTk.PhotoImage(self.resCard)        self.alien1 = self.canvas.create_oval(20, 260, 120, 360, outline='white', fill='blue')        self.alien2 = self.canvas.create_image(100, 100, image=self.CardVar, anchor=CENTER)        self.canvas.pack()        self.root.after(0, self.animation)        self.root.mainloop()     def animation(self):        track = 0        while True:            x = 5            y = 0            if track == 0:               for i in range(51):                    time.sleep(0.025)                    self.canvas.move(self.alien1, x, y)                    self.canvas.move(self.alien2, x, y)                    self.canvas.update()               track = 1               print("check")            else:               for i in range(51):                    time.sleep(0.025)                    self.canvas.move(self.alien1, -x, y)                    self.canvas.move(self.alien2, -x, y)                    self.canvas.update()               track = 0            print(track)alien()

BTW, it's not a good idea to use time.sleep in GUI programs. It puts everything to sleep, so the GUI cannot update itself, or respond to any user input. It would be better to reorganize your code so that animation uses the .after method.

Here's a version of the animation method that doesn't use sleep. You need to add self.count = 0 to the __init__ method.

 def animation(self):    y = 0    x = 5 if self.count < 50 else -5    self.count = (self.count + 1) % 100    self.canvas.move(self.alien1, x, y)    self.canvas.move(self.alien2, x, y)    self.root.after(25, self.animation)


After a while i edited the code and made it work. here is the full code

from tkinter import *import timeimport PILfrom PIL import Image,ImageFilterfrom PIL import ImageTkclass alien(object):    def __init__(self):    self.root = Tk()    self.canvas = Canvas(self.root, width=400, height = 400)    self.canvas.pack()    CardVar = ImageTk.PhotoImage(file='bubble.png')    self.alien1 = self.canvas.create_oval(20, 260, 120, 360, outline='white',fill='blue')    self.alien2 = self.canvas.create_image((100,100),image=CardVar,anchor=CENTER)    self.canvas.pack()    self.root.after(0, self.animation)    self.root.mainloop() def animation(self):    track = 0    while True:        x = 5        y = 0        if track == 0:           for i in range(0,51):                time.sleep(0.025)                self.canvas.move(self.alien1, x, y)                self.canvas.move(self.alien2, x, y)                self.canvas.update()           track = 1           print("check")        else:           for i in range(0,51):                time.sleep(0.025)                self.canvas.move(self.alien1, -x, y)                self.canvas.move(self.alien2, -x, y)                self.canvas.update()           track = 0        print(track)alien()

The problem

the cardVar was never able to find the correct image even though it was in the same folder, so i tried to do cardVar.show() and another image that was located far away showed up so it seems like something was saved in my memory. so i decided to restart kernel and everything went smooth