本文介绍了如何将图像插入按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在执行以下代码
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')
我正在将Pillow用于Python33.我正在尝试将图像插入按钮,但是返回以下错误消息:
I am using Pillow for Python33. I'm trying to insert an image into a button, but returns this error message:
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'
推荐答案
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()
这篇关于如何将图像插入按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!