Как вставить изображение в кнопку?

Я выполняю следующий код

import tkinter
import tkinter.messagebox
import random
from PIL import Image

item = tkinter.Button(root,
                text=color,
                width=20,
                height=10,
                relief='raised',
                borderwidth=5,
                bg=color
            )

original = Image.open('images/img1.gif')
ph_im = Image.PhotoImage(original)
item.config(image=ph_im)
item.pack(side='left')

Я использую подушку для Python33. Я пытаюсь вставить изображение в кнопку, но возвращает это сообщение об ошибке:

Traceback (most recent call last):   File "C:\Python33\projects\svetofor\index2.py", line 94, in <module>
    Application(root)   File "C:\Python33\projects\svetofor\index2.py", line 20, in __init__
    self.make_widgets()   File "C:\Python33\projects\svetofor\index2.py", line 50, in make_widgets
    ph_im = Image.PhotoImage(original) AttributeError: 'module' object has no attribute 'PhotoImage'

person Sergey    schedule 08.02.2014    source источник


Ответы (1)


PhotoImage находится в PIL.ImageTk.

import tkinter
import tkinter.messagebox
import random
from PIL import Image, ImageTk # <---

root = tkinter.Tk()
color = 'white'

item = tkinter.Button(root,
                text=color,
                width=20,
                height=10,
                relief='raised',
                borderwidth=5,
                bg=color
            )

original = Image.open('images/img1.gif')
ph_im = ImageTk.PhotoImage(original) # <----------
item.config(image=ph_im)
item.pack(side='left')
root.mainloop()

введите здесь описание изображения

person falsetru    schedule 08.02.2014