【问题标题】:displaying an Image in python within a function [duplicate]在函数中显示python中的图像[重复]
【发布时间】:2020-10-08 21:55:03
【问题描述】:

我正在尝试从链接中显示图像,下面的代码可以正常工作并按预期显示图像

from tkinter import *
from PIL import ImageTk,Image
import requests
from io import BytesIO

root = Tk()
root.title('Weather')
root.iconbitmap('icon.ico')
root.geometry("450x300")

image_link = requests.get('https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0006_mist.png')
my_img = ImageTk.PhotoImage(Image.open(BytesIO(image_link.content)))
image_link  =Label(root, image = my_img)
image_link.grid(row = 0, column = 0 )

root.mainloop()

但现在我必须稍微更新一下我的代码,并且必须把它放在一个函数中

from tkinter import *
from PIL import ImageTk,Image
import requests
from io import BytesIO

root = Tk()
root.title('Weather')
root.iconbitmap('icon.ico')
root.geometry("450x300")

def image_func():
    image_link = requests.get('https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0006_mist.png')
    my_img = ImageTk.PhotoImage(Image.open(BytesIO(image_link.content)))
    image_label  =Label(root, image = my_img)
    image_label.grid(row = 0, column = 0 )

image_func()
root.mainloop()

上面没有显示图像,所以我尝试将图像标签放在框架内,但这只是显示框架内没有任何内容,powershell 或 cmd 也没有显示任何错误

【问题讨论】:

    标签: python image tkinter


    【解决方案1】:

    您在函数内创建的所有对象都是本地的。所以这些对象在函数退出时被删除。您需要一种方法来保持这些对象存在。例如通过使用全局变量

    from tkinter import *
    from PIL import ImageTk,Image
    import requests
    from io import BytesIO
    
    root = Tk()
    root.title('Weather')
    root.iconbitmap('icon.ico')
    root.geometry("450x300")
    
    my_img = None
    image_label = None
    
    def image_func():
        global my_img, image_label
    
        image_link = requests.get('https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0006_mist.png')
        my_img = ImageTk.PhotoImage(Image.open(BytesIO(image_link.content)))
        image_label  = Label(root, image = my_img)
        image_label.grid(row = 0, column = 0 )
    
    image_func()
    
    root.mainloop()
    

    另一种方法是从函数中返回对象并保留返回值

    from tkinter import *
    from PIL import ImageTk, Image
    import requests
    from io import BytesIO
    
    root = Tk()
    root.title('Weather')
    root.iconbitmap('icon.ico')
    root.geometry("450x300")
    
    def image_func():
            
        image_link = requests.get('https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0006_mist.png')
        my_img = ImageTk.PhotoImage(Image.open(BytesIO(image_link.content)))
        image_label = Label(root, image=my_img)
        image_label.grid(row=0, column=0)
    
        return my_img, image_label
    
    
    my_img, image_label = image_func()
    
    root.mainloop()
    

    【讨论】:

    • 谢谢它确实有效,但这段代码是它的略读版本,上面还有其他标签和按钮,但所有这些都可以正常工作,只是有问题的图像其余工作正常,所以它无论如何,我没有想到 global 将是解决方案,谢谢:)
    • @sarfarazsaleem 基本思想是保持对图像的引用,这样它就不会被 python 的垃圾收集器清除。您甚至可以说 image_label.imag = my_img 或 global my_img` 或创建一个列表并附加到它。您所要做的就是保留对图像的引用
    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2011-03-22
    相关资源
    最近更新 更多