【问题标题】:python3.2 tkinter display an image from a URLpython3.2 tkinter 显示来自 URL 的图像
【发布时间】:2013-06-06 16:28:10
【问题描述】:

我正在尝试在 python 3.2 上使用 tkinter 来显示来自 Internet 的图像。

我收到 TypeError: 'HTTPResponse' object is not callable。我知道这意味着我错误地引用了一个变量或函数。 我已经阅读了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()

【问题讨论】:

    标签: python-3.x tkinter


    【解决方案1】:

    您可以改为创建StringIO 对象:

    from StringIO import StringIO
    
    ...
    
    im = StringIO(img_file.read())
    

    它的行为就像一个文件,但它不是一个文件。

    【讨论】:

    • 感谢您的快速回复,但是当我运行代码时出现错误:UnsupportedOperation:Seek。我用谷歌搜索了这个错误,大多数点击都带有 I/O 问题,但我远非专家。你的帮助是最有价值的。这是完整的错误:pastebin.com/Ney0Lf0G
    • @pleabargain:奇怪。尝试使用StringIO
    猜你喜欢
    • 1970-01-01
    • 2021-06-26
    • 2016-11-05
    • 1970-01-01
    • 2014-06-04
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多