Tkinter图像功能不返回任何内容

时间:2018-03-19 19:18:16

标签: python image tkinter

我的问题是,当我致电image1时,它可以工作,但根本不显示任何图像

Not showing image

以下是该计划的完整代码:

from Tkinter import *
from PIL import Image, ImageTk
import random
root = Tk()
n = random.randint(1,1)
def image1():
    image = Image.open("400.jpg")
    image = image.resize((500, 250), Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image)
def image2():
    image = Image.open("236.jpg")
    image = image1.resize((500, 250), Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image2)
def image3():
    image = Image.open("258.jpg")
    image = image1.resize((500, 250), Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image3)
def image4():
    image = Image.open("20130818_145719.jpg")
    image = image1.resize((500, 250), Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image4)
def image5() :
    image = Image.open("DSC01190.jpg")
    image = image1.resize((500, 250), Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image5)
def image6():
    image = Image.open("DSC01472.jpg")
    image = image1.resize((500, 250), Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image6)
def image7():
    image = Image.open("IMG_0029.jpg")
    image = image1.resize((500, 250), Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image7)
def image8():
    image = Image.open("IMG_0204.jpg")
    image = image1.resize((500, 250), Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image8)


label = Label(image=image1()) # keep a reference!
label.pack()
root.mainloop()

1 个答案:

答案 0 :(得分:0)

添加:

...
reference_to_the_image = image1()
label = Label(image=reference_to_the_image)
...

由于图像的引用需要存在于范围内,因此需要显示在其中。