17. Importación desde internet

Este programa nos permite obtener y mostrar una imagen de la web, para poder mostrarla es necesario que tenga la extensión .gif


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("Mostrar imagen")
# margenes
w = 520
h = 300
x = 100
y = 100
# usar width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (w, h, x, y))
# cualquier imagen de la red
image_url = "https://steamuserimages-a.akamaihd.net/ugc/951841726604632846/8C089397379FB798EA78ADCA9ABF74393E6F7ADC/"
image_byt = urlopen(image_url).read()
image_b64 = base64.encodestring(image_byt)
photo = tk.PhotoImage(data=image_b64)
# SE CREA UN CANVAS BLANCO
cv = tk.Canvas(bg='white')
cv.pack(side='top', fill='both', expand='yes')
# SE COLOCA LA IMAGEN EN EL CANVAS
# CREAR_IMAGEN(xpos, ypos, image, anchor)
cv.create_image(10, 10, image=photo, anchor='nw')
root.mainloop()



Comentarios

Entradas populares de este blog

3. Suma, resta y multiplicaión en Tkinter

2. Creación de figuras con Tkinter

Evaluación de la Unidad 1: Introducción a los ambientes de graficación (TEORÍA)