1

I'm new to python and I'm on a windows 7 64 bit with python 3.3. I can display a gif image with the following code. However I can't make it work with png files. How to do that? Thanks.

import urllib
import urllib.request
import tkinter as tk
root = tk.Tk()
url = "http://www.baidu.com/img/bdlogo.gif"
u = urllib.request.urlopen(url)
raw_data = u.read()
u.close()
import base64
b64_data = base64.encodestring(raw_data)
image = tk.PhotoImage(data=b64_data)
label = tk.Label(image=image)
label.pack()
4

1 回答 1

2

您应该使用 PIL(或枕头)。你可以在这里找到枕头窗口二进制文件。

安装枕头后尝试以下示例:

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()
于 2013-09-03T17:07:43.230 回答