python tkinter新手疑惑
#!/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新手,想问一下为什么倒数第二行加了之后原本正常显示的图片显示不出来了
相关推荐
Windows下python3安装tkinter的问题及解决方法
主要介绍了Windows下python3安装tkinter问题及解决方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
Python GUI之tkinter窗口视窗教程大集合(推荐)
一、前言 由于本篇文章较长,所以下面给出内容目录方便跳转阅读,当然也可以用博客页面最右侧的文章目录导航栏进行跳转查阅。 二、Tkinter是什么 Tkinter 是使用 python 进行窗口视窗设计的模块。Tkinter模块(“Tk 接口”)是Python的标准Tk GUI工具包的接口。作为 python 特定的GUI界面,是一个图像的窗口,tkinter是python 自带的,可以编辑的GUI界面,我们可以用GUI 实现很多直观的功能,比如想开发一个计算器,如果只是一个程序输入,输出窗口的话,是没用用户体验的。所有开发一个图像化的小窗口,就是必要的。 对于稍有GUI编程经验的人
详解python tkinter模块安装过程
tkinter其实是Python调用tcl程序的标准Python程序,可以通过这个interface调用tcl的程序,因为在大多数的unix系统中都内置了很多的tcl程序和命令。这篇文章主要介绍了python tkinter模块安装,需要的朋友可以参考下
Python TKinter如何自动关闭主窗口
主要介绍了Python TKinter如何自动关闭主窗口,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
python Tkinter的图片刷新实例
今天小编就为大家分享一篇python Tkinter的图片刷新实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧


