'''Tk_Canvas_Image_url.py
display an image obtained from an internet web page in Tkinter
tested with Python27 and Python33  by  vagaseat   21nov2012
'''

import io
import base64
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("display a website image")
# a little more than width and height of image
w = 520
h = 320
x = 80
y = 100
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (w, h, x, y))

# 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
cv = tk.Canvas(bg='white')
cv.pack(side='top', fill='both', expand='yes')

# put the image on the canvas with
# create_image(xpos, ypos, image, anchor)
cv.create_image(10, 10, image=photo, anchor='nw')

root.mainloop()
5
Contributors
5
Replies
24
Views
4 Years
Discussion Span
Last Post by Taylor_6
1

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()

Edited by Ene Uran

0

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.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.