Danny
Danny

Reputation: 384

tkinter create_image not working

I have been trying to fix this bug for hours and I'm completely lost. I'm trying to draw an image to my tkinter canvas. The canvas is first created when I initialize a class using the following code:

self.obj = tk.Tk()
self.screen = tk.Canvas(self.obj, bg='black', height='320', width='640')
self.screen.pack()
self.pixel = tk.PhotoImage(file="pixel.gif")
self.obj.mainloop()

Then in a later function, I try to draw self.pixel to the canvas using this code:

 self.screen.create_image((160, 320), image=self.pixel, anchor = tk.CENTER)

This statement definitely executes but nothing is drawn to the canvas. When I exit out of the tkinter window the following traceback gets printed to the console:

File "c:\Python33\lib\tkinter\__init__.py", line 2284, in create_image
    return self._create('image', args, kw)
File "c:\Python33\lib\tkinter\__init__.py", line 2275, in _create
    *(args + self._options(cnf, kw))))
_tkinter.TclError: invalid command name ".43421368"

The name of the "invalid command" changes every time I run the program but the rest of the error message stays the same.

Thank you for your help.

Upvotes: 1

Views: 1355

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385970

_tkinter.TclError: invalid command name ".43421368" means that you are trying to call a method on a widget that has been destroyed.

The way you wrote your question, it sounds like you have code that executes after mainloop() is called and exits. Make sure all your code executes before mainloop() returns.

Upvotes: 2

Eric Levieil
Eric Levieil

Reputation: 3574

Have you tried:

self.screen.create_image(160, 320, image=self.pixel, anchor = tk.CENTER)

Upvotes: -1

Related Questions