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()
答案 0 :(得分:2)
错误消息,subsample
和zoom
在ImageTk.PhotoImage
中不可用,且仅在Tkinter.PhotoImage
中可用。后者只接受PPM,PGM,GIF,如果你使用的是Tk 8.6b2或更高版本的Python(目前不太可能),那么也支持PNG图像。
答案 1 :(得分:0)
也许这段代码可以解决问题 - 我成功地尝试了它:
# http://effbot.org/imagingbook/image.htm
import Tkinter
# from PIL
import Image, ImageTk
im = Image.open('z:/g1.jpg')
# imc=im.copy()
imc=im.transpose(Image.ROTATE_270)
(x0,y0,x1,y1)=imc.getbbox() # returns (0,0,w,h)
print 'x0=%d y0=%d x1=%d y1=%d\n' % (x0,y0,x1,y1)
tkroot=Tkinter.Tk()
# tki=ImageTk.PhotoImage(im)
# tkl=Tkinter.Label(tkroot, image=tki)
# tkl.pack()
imc.thumbnail((1+x1/5,1+y1/5)) # changes image in place!
tkic=ImageTk.PhotoImage(imc)
tklc=Tkinter.Label(tkroot,image=tkic)
tklc.pack()
tkroot.mainloop() # Start the GUI