Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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
不能在tkinter的画布中显示多个图像_Tkinter_Python Imaging Library_Tkinter Canvas - Fatal编程技术网

不能在tkinter的画布中显示多个图像

不能在tkinter的画布中显示多个图像,tkinter,python-imaging-library,tkinter-canvas,Tkinter,Python Imaging Library,Tkinter Canvas,我正在尝试一个接一个地创建该系列中的图像渲染。但通常它显示最后一个图像和其他图像是不可见的。我发现引用变量(cj.img=t)存在问题,该变量每次都保持不变。我应该怎么做 import tkinter from tkinter import * import PIL from PIL import Image,ImageTk #------------------------------------------------------------------------------------

我正在尝试一个接一个地创建该系列中的图像渲染。但通常它显示最后一个图像和其他图像是不可见的。我发现引用变量(cj.img=t)存在问题,该变量每次都保持不变。我应该怎么做

import tkinter
from tkinter import *
import PIL
from PIL import Image,ImageTk

#--------------------------------------------------------------------------------------------------

root = Tk()
root.geometry("600x400")

cj = Canvas(root,width = 600,height = 300,relief = SUNKEN,bd = 1,bg = "#494949")
cj.grid(row = 1,column = 0,sticky = "news")

w = 0
imh = ["myphoto.png","new2.png","format.png"]
def showimg():
     for i in range(len(imh)) :
          t = ImageTk.PhotoImage(file = imh[i])
          cj.img = t
          cj.create_image(100,(i * 50),image = t,anchor = N)

showimg()


差不多了。每个图像必须单独定义,否则picture
t
会覆盖上一个图像。所以下面的代码应该给出一个提示

from tkinter import Tk, Canvas, SUNKEN
import PIL
from PIL import Image, ImageTk


root = Tk()
root.geometry('600x400')

cj = Canvas(root, width=600, height=300, relief=SUNKEN, bd=1, bg="#494949")
cj.grid(row=1, column=0, sticky='nesw')

image_files = ['myphoto.png', 'new2.png', 'format.png']
pics = []


def show_img():
    for i, image_file in enumerate(image_files):

          pics.append(ImageTk.PhotoImage(file=image_file))
          cj.create_image(100, i * 50, image=pics[i], anchor='n')

show_img()
root.mainloop()
小贴士:

  • 不进口*
  • 在for循环中使用enumerate()获取索引i和文件名
  • 使用更具描述性的文件名
  • 根据PEP8,在逗号后添加空格

一些有用的文档:

谢谢您,先生,它对我很有用。我会按照您的建议尽量使我的程序更具描述性。谢谢,只需勾选“已回答”即可,给我一些荣誉谢谢。