0

I'm new to the turtle module and I'm having a problem while setting a background pic for my turtle project. when running this code:

import turtle as tr
import os
os.chdir(pathname)
tr.setup(400,400)
tr.bgpic("diamond.gif")

I get an error message for the 5th line ending with:

_tkinter.TclError: image "pyimage4" doesn't exist

Sometimes it's pyimage2 doesn't exist or pyimage36. At each execution it changes.

I didn't find a real solution in other posts. Any help will be much appreciated.

Manel
  • 176
  • 4
  • 16
  • Is it the only code you are trying? There are other topics here with same symptoms, like [tkinter.TclError: image “pyimage3” doesn't exist](http://stackoverflow.com/q/20251161) and [Inconsistent Python Tkinter image display](http://stackoverflow.com/q/35484425), all mentioning other circumstances. (Perhaps those are the post you were talking about.) – Anton Samsonov Jan 01 '17 at 11:41
  • 1
    I've seen them but the codes are much more complicated than mine and the solutions they suggest aren't compatible with my code. And yes it's the only code I'm running. At first I tried it with a longer code in which I make the turtle move. But since the code works fine without setting the bgpic I thought it was irrelevant to put the long code here. – Amine Chaker Jan 01 '17 at 11:48

1 Answers1

0

You're not showing us your actual minimal code that fails as your example doesn't get past this line:

os.chdir(pathname)

since pathname isn't defined. I downloaded this GIF, renamed it diamond.gif, and ran the following subset of your code:

import turtle as tr
tr.setup(400, 400)
tr.bgpic("diamond.gif")
tr.done()

This displays the GIF in a window:

enter image description here

If you repeat what I did, and it works, then this may be a problem with your GIF file. (Download the GIF from this link, don't use my PNG illustration above.) If you repeat what I did and it doesn't work, then it may be a problem with your environment. This error message:

_tkinter.TclError: image "pyimage4" doesn't exist

is often associated with independently initializing both the turtle and the tkinter modules. If you aren't doing such, perhaps you're running in a specially tweaked environment that is. There may be a workaround, but you first need to determine what's really happening.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • pathname is just there to replace the actual path. I tried with your image but I still get the same error message. It must be an environment problem as you say. What can be the solution to that? – Amine Chaker Jan 01 '17 at 19:36
  • @AmineChaker, I assume you tried it with the code I supplied, not yours, and it failed. If so, now you need to describe your environment. That is, what type of system are you using: Mac, PC, Linux, etc. What version of Python. Are you using some IDE like IDLE to develop with or are you invoking Python at the command line. Don't respond to this comment with this information, edit your question to add this information to help others help you. – cdlane Jan 01 '17 at 19:53