我正在尝试在python 3.2上使用tkinter来显示来自Internet的图像。
我收到TypeError:'HTTPResponse'对象不可调用。我知道这意味着我正在引用变量或函数错误。 我已阅读urllib "module object is not callable"但我仍不确定如何解决此问题。感谢您的帮助。
import tkinter as tk
from urllib.request import urlopen
from PIL import ImageTk, Image
#http://docs.activestate.com/activepython/3.1/diveintopython3/html/porting-code- to-python-3-with-2to3.html
import io
img_file = urlopen('https://www.google.com/images/srpr/logo4w.png')
im = io.FileIO(img_file())<<<<<<<<<<<<<this line is throwing the error
resized_image = Image.open(im)
im.show()
root = tk.Tk()
img = ImageTk.PhotoImage(resized_image)
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
答案 0 :(得分:0)
您可以改为创建StringIO
对象:
from StringIO import StringIO
...
im = StringIO(img_file.read())
它的行为类似于文件,但它不是文件。