-1

I'm using python's Turtle Graphics and I want to set a background picture. However, it's not working. I need an answer quick as I have an assignment for my Computer Science class tomorrow >_<. Here's my code:

import time
import sys
import turtle

##Render
turtle.bgpic("background.png")

##End
turtle.done()

And I'm getting this error:

Traceback (most recent call last):
   File "C:/Users/Alfie/Desktop/Youtube Game/Youtube.py", line 6, in <module>
    turtle.bgpic("background.png")
   File "<string>", line 8, in bgpic
   File "C:\Python27\lib\lib-tk\turtle.py", line 1397, in bgpic
    self._bgpics[picname] = self._image(picname)
   File "C:\Python27\lib\lib-tk\turtle.py", line 503, in _image
    return TK.PhotoImage(file=filename)
   File "C:\Python27\lib\lib-tk\Tkinter.py", line 3366, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
   File "C:\Python27\lib\lib-tk\Tkinter.py", line 3320, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't recognize data in image file "background.png"

Followed with a Not Responding screen. Anyone know what the error is?

kichik
  • 33,220
  • 7
  • 94
  • 114
A Display Name
  • 367
  • 1
  • 9
  • 18

2 Answers2

1

Tk only supports GIF, PGM and PPM according to this question. Your turtle library uses Tk internally and so you have to use a GIF file for your background.

Community
  • 1
  • 1
kichik
  • 33,220
  • 7
  • 94
  • 114
  • 1
    In fact the documentation for the turtle package claims it only supports .gif images for the background in [.bgpic](https://docs.python.org/2/library/turtle.html#turtle.bgpic). – Steve Cohen Apr 14 '16 at 17:49
0

I have tried that function.

  1. I have used format .png --> OK
  2. Remember \\ instead of \ ex: turtle.bgpic("""C:\\Users\\ASUS\\Downloads\\test.gif""") --> So I think at that time, we can use .png in Python turtle.