Tkinter.PhotoImage doesn't not support png image

前端 未结 6 865
礼貌的吻别
礼貌的吻别 2020-11-27 08:51

I am using Tkinter to write a GUI and want to display a png file in a Tkiner.Label. So I have some code like this:

self.vcode.img = PhotoImage(d         


        
6条回答
  •  忘掉有多难
    2020-11-27 09:03

    PIL is now replaced by Pillow http://pillow.readthedocs.io/en/3.2.x/

    solution:

    from Tkinter import *
    import PIL.Image
    import PIL.ImageTk
    
    root = Toplevel()
    
    im = PIL.Image.open("photo.png")
    photo = PIL.ImageTk.PhotoImage(im)
    
    label = Label(root, image=photo)
    label.image = photo  # keep a reference!
    label.pack()
    
    root.mainloop()
    

    If PIL could not be found in code, you do need a pillow install:

    pip install pillow
    

提交回复
热议问题