Python 如何将图像插入按钮?
我正在执行以下代码Python 如何将图像插入按钮?,python,python-3.x,tkinter,python-3.3,pillow,Python,Python 3.x,Tkinter,Python 3.3,Pillow,我正在执行以下代码 import tkinter import tkinter.messagebox import random from PIL import Image item = tkinter.Button(root, text=color, width=20, height=10, relief='raised', bord
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')
我正在用枕头做蟒蛇33。我试图将图像插入按钮,但返回以下错误消息:
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'
Traceback(最近一次调用last):文件“C:\Python33\projects\svetofor\index2.py”,第94行,在
应用程序(根)文件“C:\Python33\projects\svetofor\index2.py”,第20行,在\uuu init中__
make_widgets()文件“C:\Python33\projects\svetofor\index2.py”,第50行,在make_widgets中
ph_im=Image.PhotoImage(原始)AttributeError:“模块”对象没有属性“PhotoImage”
在模块中
导入tkinter
导入tkinter.messagebox
随机输入
从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()