简体   繁体   中英

Error while setting a bgpic with turtle

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.

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:

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM