0 votes

Tkinter cannot recognize images

I'm trying to put an image in a label of an interface, but when I run the program I get this error

Traceback (most recent call last): File " C:\Users\Familia\Desktop\Archivos programming \Programaci s website \proyecto F \prueba tkinter ventana.pyw", line 18, in image=Tkinter.PhotoImage(file="tec.jpg") File " C:\Python27\lib\lib -tk \Tkinter.py ", line 3377, in init Image. init (self, 'photo', name, cnf, master, **kw) File " C:\Python27\lib\lib -tk \Tkinter.py ", line 3331, in init self.tk.call(('image', 'create', imgtype, name,) + options) TclError: couldn't recognize data in image file "tec.jpg"

This is my code

import Tkinter

ventana= Tkinter.Tk()
ventana.geometry("500x500")
ventana.title("Pruebas sudoku")
ventana.resizable(0, 0)
ventana.iconbitmap("logo1.ico")

miframe=Tkinter.Frame(ventana)
miframe.pack()
dato=Tkinter.StringVar()
caja1=Tkinter.Entry(ventana, textvariable=dato)
caja1.place(x=150, y=10)

ltexto=Tkinter.Label(ventana, text="etiqueta texto", font=18)
ltexto.place(x=50, y=10)

imagen=Tkinter.PhotoImage(file="tec.jpg")
limagen=Tkinter.Label(miframe, image = imagen)
limagen.image = imagen
ventana.mainloop()

P.S: I already tried to put the image path, use other images and put the label in the window and not in the frame.

I am using pthon 2.7

0voto

Firefly Points 1319

The class PhotoImage from tkinter supports the following formats: PGM, PPM, GIF, PNG. If you want to work with other formats, you could use Python Imaging Library (PIL) which contains classes that allow you to use about 30 formats, converting them into Tkinter-compatible objects. PIL you could use it in the following way:

from PIL import Image, ImageTk
imagen = Image.open("tec.jpg")
foto = ImageTk.PhotoImage(imagen)
limagen=tkinter.Label(miframe, image = foto)
limagen.image = imagen
limagen.pack()

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X