This code shows how to obtain and display a GIF image from an internet website using the Tkinter GUI toolkit that comes with the Python installation.
You can use the canvas image as a background to your turtle drawings:
''' turtle_Tk_Canvas_Image_url.py display a GIF image obtained from an internet web page on a Tkinter canvas, then use the canvas for some turtle drawing modified Vegaseat code ''' import io import base64 import turtle try: # Python2 import Tkinter as tk from urllib2 import urlopen except ImportError: # Python3 import tkinter as tk from urllib.request import urlopen root = tk.Tk() root.title("turtle graphics a website image") # this GIF picture previously downloaded to tinypic.com image_url = "http://i46.tinypic.com/r9oh0j.gif" image_byt = urlopen(image_url).read() image_b64 = base64.encodestring(image_byt) photo = tk.PhotoImage(data=image_b64) # create a white canvas large enough to fit the image+ w = 540 h = 340 cv = tk.Canvas(bg='white', width=w, height=h) cv.pack() # this makes the canvas a turtle canvas # point(0, 0) is in the center now tu = turtle.RawTurtle(cv) # put the image on the turtle canvas with # create_image(xpos, ypos, image, anchor) xpos = int(w/2 * 0.9) ypos = int(h/2 * 0.9) print(xpos, ypos) cv.create_image(-xpos, -ypos, image=photo, anchor='nw') # now do some turtle graphics tu.pensize(2) for radius in range(50, 200, 40): # pen up tu.up() # move pen to point x, y # keeps the center of the circle at canvas center tu.goto(0, -radius) # pen down tu.down() tu.circle(radius) root.mainloop()
It's working, but it's not animated, I simply copy-pasted the code, didn't change anything, then changed the image, but still not animating.
When i tryed this myself with my own url, it said:
Traceback (most recent call last):
File "/Users/GAMEKNIGHT7/Documents/chineseCheckersAI(genius hour).py", line 14, in <module>
image_byt = url(image_url).read()
NameError: name 'url' is not defined
Is there something wrong with the module? When i tested it with your url, it worked fine.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.