2014-02-08 3 views
1

다음 코드 내가 Python33 위해 베개를 사용하고이미지를 버튼에 삽입하는 방법은 무엇입니까?

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') 

을 실행하고 있습니다. 나는 버튼에 이미지를 삽입하려고하지만,이 오류 메시지를 반환하고 있습니다 :

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' 

답변

1

PhotoImagePIL.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() 

enter image description here

관련 문제