12,063
社区成员
发帖
与我相关
我的任务
分享
#!/usr/bin/python3.5
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
root = Tk()
root.title("Title")
root.geometry('600x600')
def resize_image(event):
width = int(event.width * new_width)
height = int(event.height * new_height)
image = copy_of_image.resize((width, height))
photo = ImageTk.PhotoImage(image)
label.config(image=photo)
label.image = photo #avoid garbage collection
def resize(w_box, h_box, pil_image):
'''
resize a pil_image object so it will fit into
a box of size w_box times h_box, but retain aspect ratio
对一个pil_image对象进行缩放,让它在一个矩形框内,还能保持比例
'''
w, h = pil_image.size
f1 = 1.0 * w_box / w # 1.0 forces float division in Python2
f2 = 1.0 * h_box / h
factor = min([f1, f2])
# print(f1, f2, factor) # test
# use best down-sizing filter
width = int(w * factor)
height = int(h * factor)
return pil_image.resize((width, height), Image.ANTIALIAS)
image = Image.open('right.jpg')
pil_image = resize(root.winfo_screenwidth()*0.195, root.winfo_screenheight()*0.347, image)
w, h = pil_image.size
copy_of_image = pil_image.copy()
new_width = 1.0 * w / 600
new_height = 1.0 * h / 600
photo = ImageTk.PhotoImage(pil_image)
label = ttk.Label(root, image = photo)
label.bind('<Configure>', resize_image)
label.pack(fill=BOTH, expand = YES)
#label.place(x=300, y=300)
root.mainloop()
是tkinter新手,想问一下为什么倒数第二行加了之后原本正常显示的图片显示不出来了