user88720
user88720

Reputation: 342

How to add a URL image to Tkinter in Python 2.7 using only the standard Python library?

I have seen many examples of how to show images in Tkinter using an image URL but none of those examples are working for me i.e.

import urllib 
from Tkinter import *
import io
from PIL import Image, ImageTk


app = Tk()
app.geometry("1000x800")

im = None  #<-- im is global

def search():
    global im  #<-- declar im as global, so that you can write to it
               # not needed if you only want to read from global variable.
    tx1get = tx1.get()
    Label(app, text="You Entered: \"" + tx1get + "\"").grid(row=1, column=0)
    fd = urllib.urlopen("http://ia.media-imdb.com/images/M/MV5BMTc2MTU4ODI5MF5BMl5BanBnXkFtZTcwODI2MzAyOA@@._V1_SY317_CR7,0,214,317_AL_.jpg")
    imgFile = io.BytesIO(fd.read())
    im = ImageTk.PhotoImage(Image.open(imgFile))
    image = Label(app, image = im, bg = "blue")
    image.grid(row=2, column=0)

tx1=StringVar()
tf = Entry(app, textvariable=tx1, width="100")
b1 = Button(app, text="Search", command=search, width="10")
tf.grid(row=0, column=0)
b1.grid(row=0, column=1)

app.mainloop()

When I run this I get the error "No module name PIL" And in this one:

from io import BytesIO
import urllib
import urllib.request
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
url = "http://imgs.xkcd.com/comics/python.png"
with urllib.request.urlopen(url) as u:
    raw_data = u.read()
im = Image.open(BytesIO(raw_data))
image = ImageTk.PhotoImage(im)
label = tk.Label(image=image)
label.pack()
root.mainloop()

I get "No module name request" Nearly all examples make use of the PIL module among others, but I cannot get them to work because Python 2.7 doesn't recognize many of them. I need to display an image as a part of an assessment and while we can import things such as Tkinter, the file needs to run without having to add modules from outside the standard Python library.

It is worth noting as well that I can't even import "tkinter". It will say there is no module named "tkinter" because it needs to start with a capital "T".

So my questions are:

  1. Does PIL need me to install additional software/libraries

  2. Does the importation of "tkinter" without the capital "T" not work because I am using Python 2.7?

  3. Using Python 2.7 how do I display an image in a Tkinter window from a URL

Upvotes: 1

Views: 2679

Answers (1)

Khristos
Khristos

Reputation: 983

This works, using python 2.7 on windows:

from io import BytesIO
import Tkinter as tk
import urllib  # not urllib.request
from PIL import Image, ImageTk

root = tk.Tk()
url = "http://imgs.xkcd.com/comics/python.png"

u = urllib.urlopen(url)
raw_data = u.read()
u.close()

im = Image.open(BytesIO(raw_data))
image = ImageTk.PhotoImage(im)
label = tk.Label(image=image)
label.pack()
root.mainloop()

To answer your questions:

  1. You need to install PIL (it's not standard on Python 2.7).
  2. Yes, you need to import Tkinter in Python 2.7; tkinter is for Python 3.x
  3. You can use the code above (provided you install PIL).

Also,

  1. In Python 2.7 you need urllib and not urllib.request
  2. Seems you can't use with....open(x) as fnamein urllib so you need to open and close the file explicitly.

Upvotes: 1

Related Questions