Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
create_image()函数python只显示循环中的最后一个图像_Python_Python 3.x_Tkinter_Tkinter Canvas - Fatal编程技术网

create_image()函数python只显示循环中的最后一个图像

create_image()函数python只显示循环中的最后一个图像,python,python-3.x,tkinter,tkinter-canvas,Python,Python 3.x,Tkinter,Tkinter Canvas,我试图创建一个程序,在循环中显示一系列图像,但是create_image()函数只绘制我尝试绘制的最后一幅图像 for i in range(len(imgList)): filename = PhotoImage(file = str(imgList[i]).lower() + ".png") C.create_image(45 * (i + 1), 5, anchor=NE, image=filename) print("created image") 无论需要多

我试图创建一个程序,在循环中显示一系列图像,但是create_image()函数只绘制我尝试绘制的最后一幅图像

for i in range(len(imgList)): 
    filename = PhotoImage(file = str(imgList[i]).lower() + ".png")
    C.create_image(45 * (i + 1), 5, anchor=NE, image=filename)
    print("created image")
无论需要多少次,它都会打印“创建的图像”,但只显示最后一个图像。

imgList只是所有图像的名称

在tkinter中显示图像时,您需要保留对分配给它们的变量的引用,否则它们会被垃圾收集。请尝试以下操作:

c.images = list()
for i in range(len(imgList)): 
    image = PhotoImage(file = str(imgList[i]).lower() + ".png")
    c.create_image(45 * (i + 1), 5, anchor=NE, image=image)
    c.images.append(image)
    print("created image")
您应该查看并相应地编辑您的问题