[pyar] Cargar diferentes formatos de imagen en tkinter.ttk

Ernesto Savoretti esavoretti en gmail.com
Jue Nov 25 22:40:22 ART 2010


El día 25 de noviembre de 2010 20:50, craf <prog en vtr.net> escribió:
> Hola.
>
> Estoy trabajando con tkinter.ttk y probando los estilos y temas que
> vienen con la versión 8.5
>
> Con este pequeño código....
>
> ------------------------------------------------------------
> from tkinter import *
> from tkinter import ttk
> import image
>
> master = Tk()
> s = ttk.Style()
> s.theme_use('clam')
>
> foto = PhotoImage(file='mi_imagen.gif')
> b = ttk.Button(master,compound='left',image=foto,text="Hola
> Mundo").pack()
>
> master.mainloop()
> -----------------------------------------------------------
>
> ....estoy probando que imágenes se pueden cargar.
> Según el manual de tkinter.ttk, solo se pueden cargar imágenes "gif",
> pero leí que también se pueden trabajar otro tipo de formato de imágenes
> con la librería PIL. El problema es que esta librería parece que trabaja
> solo hasta python 2.6 y en windows (Utilizo Ubuntu).
>

No, no parece ser así. Mirá (errores incluídos)

Python 2.7 (r27:82500, Oct  6 2010, 12:29:13)
[GCC 4.5.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> img = Image.open("2.jpg")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Image' is not defined
>>> img = PIL.Image.open("2.jpg")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Image'
>>> dir(PIL)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
>>> from PIL import Image
>>> img = PIL.Image.open("2.jpg")
>>> img.show()
>>>

y la mostró (creeme)

1) Estamos en ArchLinux
2) Python 2.7
3) Imagen jpg

Obviamente, hay alternativas superadoras a PIL, como el port Python de
ImageMagick, pero PIL anda.

Saludos.
-- 

                                    Ernesto Savoretti



More information about the pyar mailing list