【发布时间】:2018-07-14 08:29:54
【问题描述】:
我正在使用 PIL 创建图像,而不是将它们保存在任何地方,因为它们每次都会有所不同,并试图将这些图像放在按钮上。问题是,由于某种原因,只有最后一个图像被放在按钮上。这是我的代码:
from PIL import ImageDraw
from PIL import ImageFont
from Tkinter import *
master = Tk()
W, H = (70,70)
from PIL import Image
from PIL import ImageTk
def fun(meth):
print meth
return
for i in range(0,5):
img = Image.new("RGB", (W, H), (255,0,0))
draw = ImageDraw.Draw(img)
text_string = str(i)+','+str(i+1)
font = ImageFont.truetype("arial.ttf", 25)
w, h = draw.textsize(text_string, font=font)
draw.text(((W-w)/2,(H-h)/2), str(i)+','+str(i+1),(0,0,0), font=font )
imagetk = ImageTk.PhotoImage(img)
b = Button(master, image=imagetk, command=lambda method=text_string: fun(method))
b.grid(row=0, column=i, padx=20)
#img.save(path+'.png', "PNG")
master.mainloop()
我在循环结束时将图像保存到磁盘,我看到其他图像按预期创建,但由于某种原因,它们没有放在其他按钮上。另外我刚刚注意到其他按钮甚至不响应点击,只有最后一个按预期运行。
【问题讨论】:
标签: python python-2.7 tkinter python-imaging-library