伙计们,我正在尝试使用 PhotoImage 类中的 subsample 方法来调整我在列表中的图像大小,该列表链接到 Tkinter 中的 Label 小部件,但 python 说没有这样的方法。我知道我可以在调用 PhotoImage 之前使用 Image.resize,但由于我想随时调整图像大小,所以一旦将其转换为 PhotoImage,我不知道该怎么做。有人可以告诉我我做错了什么吗?我对 Tkinter 比较陌生,对 PIL 也很陌生……谢谢……
from Tkinter import *
from PIL import Image, ImageTk
class imgList(object):
def __init__(self,imgs):
self.list=[]
for i in imgs:
image=Image.open(i)
photo=ImageTk.PhotoImage(image)
self.list.append(photo)
def resize(self,num,fact):
self.list[num]=self.list[num].subsample(fact)
#image=image.resize((50,100),Image.ANTIALIAS)
#photo=ImageTk.PhotoImage(image)
#photo.subsample(2,2)
#self.list[num]=photo
class Slot(object):
def __init__(self,root,canvas,appimg,x,y):
self.root=root
self.canvas=canvas
self.appimage=appimg
self.l=Label(self.root,image=self.appimage)
self.l.place(x=x,y=y)
class View(object):
def __init__(self,canvas):
self.canvas=canvas
class win1(View):
def slotbind(self,event):
print("heloo")
self.imglist.resize(0,2)
def draw(self,root,tv):
self.canvas.delete(ALL)
self.root=root
#self.photolist=pl
self.imglist=imgList(["pic1.bmp"])
self.tv=tv
self.s1=Slot(self.root,self.canvas,self.imglist.list[0],10,10)
self.s1.l.bind("<1>",self.slotbind)
self.qbtn=Button(self.root,text="quit",command=self.quit)
self.qbtn.place(x=270,y=100)
self.qbtn.lift()
def quit(self):
self.qbtn.destroy()
self.s1.l.destroy()
self.tv[1].draw(self.root,self.tv)
class win2(View):
def draw(self,root,tv):
self.canvas.delete(ALL)
self.root=root
self.tv=tv
imglist=imgList(["pic3.bmp","pic4.bmp"])
self.s1=Slot(self.root,self.canvas,imglist.list[1],270,10)
self.qbtn=Button(self.root,text="quit2",command=self.quit)
self.qbtn.place(x=500,y=100)
self.qbtn.lift()
def quit(self):
self.qbtn.destroy()
self.s1.l.destroy()
self.tv[0].draw(self.root,self.tv)
class win3(View):
def draw(self):
pass
class App(object):
def __init__(self, width=640, height=480):
self.width = width
self.height = height
self.root = Tk()
self.root.title("tkinter_test01")
self.root.geometry("%sx%s"%(self.width, self.height))
self.canvas = Canvas(self.root, width=self.width, height=self.height)
self.theviews=[win1(self.canvas),win2(self.canvas),win3(self.canvas)]
self.theviews[0].draw(self.root,self.theviews)
self.canvas.pack()
self.root.mainloop()
app=App()