Tkinter.PhotoImage doesn't not support png image

前端 未结 6 830
礼貌的吻别
礼貌的吻别 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 08:59

    Fixed in official python.org 64-bit (only) installer for OS X. Tk version 8.6 is included out of the box. Warning: if you use homebrew, as of this post doing brew install python3 will only give you 8.5, and 8.6 is required to use png so you'll have to use official installer instead. To check which Tk you are using:

    $ python3 -c 'import tkinter; print(tkinter.TkVersion);'
    

    If it report 8.6, you are good to go.

    0 讨论(0)
  • 2020-11-27 08:59

    try with PIL library instead of converting your image to GIF, PGM, or PPM (PhotoImage) only accept these 3 formats.

    import tkinter as tk
    import PIL.Image
    import PIL.ImageTk
    
    base = tk.Tk()
    base.title("Dialy Dose")
    
    logoPath = r"C:\Users\saigopi\Downloads\logo.png"
    
    ref = PIL.Image.open(logoPath)
    photo = PIL.ImageTk.PhotoImage(im)
    
    inputEdit = tk.Label(base,text="Enter Quote")
    save = tk.Button(base,text="Save",background="green",command=save())
    logo = tk.Label(base,image=photo,text="Logo bro lite")
    quote = tk.Label(base,text="I am saying you are more than something")
    
    inputEdit.pack()
    save.pack()
    logo.pack()
    quote.pack()
    
    base.mainloop()
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-27 09:06

    Tkinter 8.6 supports png file format while tkinter 8.5 does not. If you have the option upgrade python and you should be fine to use png. If you have to use an older version of python you should use Pillow (maintained pil fork) which also works on python3.

    If you are starting a new project do not use python2 or PIL as suggested in the accepted answer, they are both depreciated technologies.

    0 讨论(0)
  • 2020-11-27 09:12

    tkinter only supports 3 file formats off the bat which are GIF, PGM, and PPM. You will either need to convert the files to .GIF then load them (Far easier, but as jonrsharpe said, nothing will work without converting the file first) or you can port your program to Python 2.7 and use the Python Imaging Library (PIL) and its tkinter extensions to use a PNG image.

    A link that you might find useful: http://effbot.org/tkinterbook/photoimage.htm

    0 讨论(0)
  • 2020-11-27 09:15
    from tkinter import *
    from tkinter import messagebox
    import os
    from PIL import Image, ImageTk
    
    
    root = Tk()
    root.geometry("1300x720")
    root.title("KEDİLERİMİZ ve KÖPEKLERİMİZ")
    class Ana:
        def __init__(self,name,roll):
            self.name = name
            self.roll = roll
    resim = Label(root,width=77,height=43,bg="blue")
    resim.place(x=730,y=10)
    o = "1.PNG"
    hu = o.find(".")
    mu = o[hu:]
    if mu == ".gif" or mu == ".png":
        img = PhotoImage(file = o)
    else:
        photo = Image.open(o)
        img = ImageTk.PhotoImage(photo)
    resim.configure(image=img,width=img.width(),height=img.height())
    resim.image = img
    
    0 讨论(0)
提交回复
热议问题