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?
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.
I have tried that function.
I have used format .png --> OK
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.
Related
Every time, even though I have the source file, the same error message appears - 'Tcl error...could not recognize data in image file'. The problem also occurs with other images present in my disk drive.
Here is the code:
from tkinter import *
window=Tk()
window.geometry("420x420")
window.title('My first GUI programme')
source='D:\\Desktop\\op.jfif'
icon=PhotoImage(file=source)
window.iconphoto(True, icon)
window.mainloop()
Here goes the error:
PS D:\Documents\pythonProject> & "C:/Program Files (x86)/Python/python.exe" "d:/Documents/pythonProject/Learning python/Practice.py"
Traceback (most recent call last):
File "d:\Documents\pythonProject\Learning python\Practice.py", line 6, in <module>
icon=PhotoImage(file=source)
File "C:\Program Files (x86)\Python\lib\tkinter\__init__.py", line 4064, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Program Files (x86)\Python\lib\tkinter\__init__.py", line 4009, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "D:\Desktop\op.jfif"
I don't know if there is any problem with my code or i need to change any settings. I would be thankful to you if you provide me any assistance.
I managed to do this by changing the file extension and file content of my image file to 'png' which worked in python.
Python Tkinter supports GIF, PGM, PPM, and PNG. So, try changing the extension of the file to one of them
i see your problem
i also have the same problem
I'm trying to change the shape of the turtle to an 8bit link image. I have the image I want saved to the same directory as my source code (you can see that with os.getcwd().) I'm wondering why I am getting this error and how to fix it. Thanks!
from turtle import Turtle, Screen
import os
print(os.getcwd())
wn = Screen()
wn.bgcolor('lightblue')
spaceship = Turtle()
spaceship.color('red')
newshape = Screen().addshape('8bitlink.png.gif')
spaceship.shape(newshape)
spaceship.penup()
The error looks like this:
Traceback (most recent call last):
File "/Users/colind/Desktop/Spaceship Game.py", line 12, in <module>
newshape = Screen().addshape('8bitlink.png.gif')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 1133, in register_shape
shape = Shape("image", self._image(name))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 479, in _image
return TK.PhotoImage(file=filename)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 3498, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "8bitlink.png.gif": no such file or directory
One issue is that you're not invoking addshape() correctly:
newshape = Screen().addshape('8bitlink.png.gif')
spaceship.shape(newshape)
As addshape() doesn't return anything, it registers the image shape under the name of the file, so you should instead do something like:
from turtle import Turtle, Screen
wn = Screen()
wn.bgcolor('lightblue')
wn.addshape('8bitlink.png.gif')
spaceship = Turtle()
spaceship.shape('8bitlink.png.gif')
spaceship.color('red')
spaceship.penup()
# ...
wn.mainloop()
This works for me when I store a GIF image under the name '8bitlink.png.gif'. You might convince yourself, and us, that you've an actual GIF image by running the Unix file command:
> file 8bitlink.png.gif
8bitlink.png.gif: GIF image data, version 89a, 24 x 24
>
You're output should be similar though not identical.
I am trying to display the image "picture.gif" in this code:
from graphics import *
import tkinter
win = GraphWin("Self Portrait", "1000", "500")
image = Image(Point(5,5), "picture.gif")
image.draw(win)
window.mainloop()
However, I keep getting this error:
Traceback (most recent call last):
File "/Users/jstorrke/Desktop/Python/graphicsProject.py", line 6, in <module>
image = Image(Point(5,5), "picture.gif")
File "/Users/jstorrke/Desktop/Python/graphics.py", line 827, in __init__
self.img = tk.PhotoImage(file=pixmap[0], master=_root)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 3394, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 3350, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "picture.gif"
The graphics module provides very minimal support for displaying images. Try putting the image on the same folder as where graphics library is and see if it helps.
Please look at this document for more information on graphics module. http://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf
_tkinter.TclError: couldn't recognize data in image file "picture.gif"
I looked at dozens of examples this error message and the cause seems to fall into two categories:
1) The file wasn't a supported type (e.g. *.jpg, *.png, *.tif) which was obvious from the extension.
2) The file used was mislabeld as a *.gif when it wasn't. (More common than I would expect.) On Unix systems you may be able to use the file command to verify your GIF:
> file p7Q6O.gif
p7Q6O.gif: GIF image data, version 89a, 520 x 416
>
An actual failure of a valid GIF file occurs if it's an animated GIF which are not supported. However, this can be a silent failure -- no error message and no image displayed.
I am getting an error whenever I try to import the tkinter module. I'm using Python 3.4 so the capital T isn't an issue.
from tkinter import *
root = Tk()
root.mainloop()
It throws back an error like:
Traceback (most recent call last):
File "<string>", line 420, in run_nodebug
File "<module1>", line 11, in <module>
File "C:\Python34\lib\tkinter\__init__.py", line 3372
t self.tk.call(('image', 'create', imgtype, name,) + options)
^
SyntaxError: invalid syntax
Thanks for the help!
The problem seems to be somehow existing in the init file itself. Just go to the file in your pc.
C:\Python34\lib\tkinter\__init__.py
Edit that init.py with Idle or whatever IDE you use. Next go to the line number 3372 and remove that unwanted character 't' from the line
t self.tk.call(('image', 'create', imgtype, name,) + options)
It'll do the work for you.
I'm trying to change the background image in a turtle window.
The code reads simply as this:
import turtle
turtle.bgpic("france53.gif")
The turtle window shows up blank and I get the following error message:
Traceback (most recent call last):
File "<ipython-input-25-45373f8f3ea2>", line 1, in <module>
bgpic("france53.gif")
File "<string>", line 1, in bgpic
File "/Users/danielcharrier/anaconda/lib/python3.4/turtle.py", line 1482, in bgpic
self._setbgpic(self._bgpic, self._bgpics[picname])
File "/Users/danielcharrier/anaconda/lib/python3.4/turtle.py", line 738, in _setbgpic
self.cv.itemconfig(item, image=image)
File "<string>", line 1, in itemconfig
File "/Users/danielcharrier/anaconda/lib/python3.4/tkinter/__init__.py", line 2416, in itemconfigure
return self._configure(('itemconfigure', tagOrId), cnf, kw)
File "/Users/danielcharrier/anaconda/lib/python3.4/tkinter/__init__.py", line 1310, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TclError: image "pyimage12" doesn't exist
The file "france53.gif" is in the working directory. What should I do to make it work?
I'm using the Anaconda distribution for Mac OS X.
Thanks!