【问题标题】:I can't show the images on the new window using Python tkinter我无法使用 Python tkinter 在新窗口上显示图像
【发布时间】:2020-11-18 20:48:24
【问题描述】:
   def selection():
        picTk = Toplevel()
        picTk.title('Photos')
        picTk.geometry('900x500')
    
        image_list = os.listdir('.')
        photoimage_list = []
        for photo in image_list:
            if photo.endswith('.gif'):
                im = Image.open(photo)
                pic = ImageTk.PhotoImage(file=im)
                photoimage_list.append(pic)
    
        n = 0
        img_show = photoimage_list[n]
        lb1 = Label(picTk,image=img_show)
        lb1.image = img_show
        lb1.place(x=10,y=10)
    
        back_btn = Button(picTk,text='<<',command=lambda:back(-1),state=DISABLED)
        back_btn.place(x=10,y=800)
    
        exit_btn = Button(picTk,text='Exit',command=picTk.destroy)
        exit_btn.place(x=300,y=800)
    
        for_btn = Button(picTk,text='>>',command=lambda:forward(0))
        for_btn.place(x=600,y=800)
        picTk.mainloop()
    
      
    
    
    tk = Tk()
    tk.title('Home')
    
    Label(tk,text='Photos of Dominic').pack(pady=10)
    Button(tk,text='View Album',command=selection).pack(pady=10)

错误:

    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Python38\lib\tkinter\__init__.py", line 1883, in __call__
        return self.func(*args)
      File "E:\Python2\photo album\photo_album.py", line 15, in selection
        pic = ImageTk.PhotoImage(file=im)
      File "C:\Python38\lib\site-packages\PIL\ImageTk.py", line 89, in __init__
        image = _get_image_from_kw(kw)
      File "C:\Python38\lib\site-packages\PIL\ImageTk.py", line 58, in _get_image_from_kw
        return Image.open(source)
      File "C:\Python38\lib\site-packages\PIL\Image.py", line 2852, in open
        prefix = fp.read(16)
    AttributeError: 'GifImageFile' object has no attribute 'read'

当我单击“查看相册”按钮时,出现了上述错误消息。我在一天之内尝试了很多次,然后出现一个没有图像的空白窗口。我不断更改并使用不同的方法,但仍然存在错误。我还搜索了不同的书籍并使用了正确的格式,但一直出现错误。我的目标是在单击“查看相册”按钮后在第二个窗口中显示图片。如果可能的话,谁能教我如何解决这个问题?我真的需要帮助来解决上述问题。

【问题讨论】:

  • 可能是 .gif 图像有问题?格式是否正确?
  • 应该是pic = ImageTk.PhotoImage(im)pic = ImageTk.PhotoImage(file=photo)
  • 我试过删除 file= 但还是不行
  • 我把所有照片都转成gif格式了
  • 删除file=后出现什么错误?

标签: python image tkinter gif


【解决方案1】:

如果我的理解是正确的,您正在使用 Image.open() 打开图像,那么您将其作为文件传递给 ImageTk.PhotoImage()。要解决此问题,您可以使用 ImageTk.PhotoImage(image=im) 或简单地删除 Image.open(photo) 并使用 ImageTk.PhotoImage(file=photo) 我希望这会有所帮助。

   def selection():
        picTk = Toplevel()
        picTk.title('Photos')
        picTk.geometry('900x500')
    
        image_list = os.listdir('.')
        photoimage_list = []
        for photo in image_list:
            if photo.endswith('.gif'):
                pic = ImageTk.PhotoImage(file=photo) # file is photo not im
                photoimage_list.append(pic)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多