Python 3-如何使用 TKINTER 从 Web 检索图像并在 GUI 中显示? [英] Python 3- How to retrieve an image from the web and display in a GUI using TKINTER?

查看:40
本文介绍了Python 3-如何使用 TKINTER 从 Web 检索图像并在 GUI 中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个函数,当一个按钮被点击时,它会使用 URLLIB 从网络中获取图像,并使用 TKINTER 将其显示在 GUI 中.

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.

我是 URLLIB 和 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)

推荐答案

我不使用 python 3,但我可以给出适用于 python 2.5+ 的答案.我认为该代码在 python 3 上的工作方式几乎相同.

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.

在开始之前,我们需要导入 Tkinter 并创建根窗口:

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

import Tkinter as tk
root = tk.Tk()

接下来,使用 urllib 下载图像:

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

您现在在变量 raw_data 中拥有图像的二进制数据.Tkinter 接受一个 data 选项,但不幸的是你不能提供这个原始数据.它期望将数据编码为 base64.这很容易做到:

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.

以上仅适用于 .gif 图像,但其他图像格式几乎同样容易处理.最简单的方法是将原始图像数据写入磁盘(或使用 urllib 直接将数据下载到文件)并在创建 PhotoImage 对象时引用该文件.当然,这只适用于 PhotoImage 类直接支持的图像格式.

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.

您的另一个选择是使用支持多种不同图像格式的 PIL(Python 图像库).该技术大致相同,只是您必须首先创建 PIL 图像,然后将其转换为 Tkinter 可用的格式.有关 PIL 的更多信息,请参阅 Python 成像库手册 以及 关于 Tkinter PhotoImage 类的 effbot 文档

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

这篇关于Python 3-如何使用 TKINTER 从 Web 检索图像并在 GUI 中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆