Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

user interface - Python 3- How to retrieve an image from the web and display in a GUI using TKINTER?

I want a function that, when a button is clicked, it will take an image from the web using URLLIB and display it in a GUI using TKINTER.

I'm new to both URLLIB and TKINTER, so I'm having an incredibly difficult time doing this.
Tried this, but it obviously doesn't work because it uses a textbox and only will display text.

 def __init__(self, root):
    self.root = root
    self.root.title('Image Retrieval Program')
    self.init_widgets()


def init_widgets(self):
    self.btn = ttk.Button(self.root, command=self.get_url, text='Get Url', width=8)
    self.btn.grid(column=0, row=0, sticky='w')

    self.entry = ttk.Entry(self.root, width=60)
    self.entry.grid(column=0, row=0, sticky='e')

    self.txt = tkinter.Text(self.root, width=80, height=20)
    self.txt.grid(column=0, row=1, sticky='nwes')
    sb = ttk.Scrollbar(command=self.txt.yview, orient='vertical')
    sb.grid(column=1, row=1, sticky='ns')
    self.txt['yscrollcommand'] = sb.set

def get_url(self):
    s = urllib.request.urlretrieve("http://www.smellymonkey.com/monkeys/images/ill-monkey.gif", "dog.gif")
    tkimage = ImageTk.PhotoImage(im)
    self.txt.insert(tkinter.INSERT, s)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't use python 3, but I can give an answer that works in python 2.5+. I assume the code will work nearly identically on python 3.

Before we get started, we need to import Tkinter and create the root window:

import Tkinter as tk
root = tk.Tk()

Next, to download the image using urllib:

import urllib
URL = "http://www.smellymonkey.com/monkeys/images/ill-monkey.gif"
u = urllib.urlopen(URL)
raw_data = u.read()
u.close()

You now have the binary data for the image in the variable raw_data. Tkinter accepts a data option, but unfortunately you can't feed it this raw data. It expects the data to be encoded as base64. This is easy enough to do:

import base64
b64_data = base64.encodestring(raw_data)
image = tk.PhotoImage(data=b64_data)

Now that we have an image, time to put it on the screen:

label = tk.Label(image=image)
label.pack()

You should now see the image on the screen.

The above only works for .gif images, but other image formats are almost as easy to handle. The simplest method is to write the raw image data to disk (or use urllib to directly download the data to a file) and reference the file when creating the PhotoImage object. Of course, this only works for image formats directly supported by the PhotoImage class.

Your other choice is to use PIL (Python Image Library) which supports many different image formats. The technique remains roughly the same, only you must first create a PIL image, then convert it to a format usable by Tkinter. For more information about PIL consult the Python Imaging Library Handbook and also the effbot documentation on The Tkinter PhotoImage Class


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...