python – 使用Tkinter显示图像
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 使用Tkinter显示图像,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3026字,纯文字阅读大概需要5分钟。
内容图文
我正在研究一个使用Tkinter和ImageTk显示一系列图像的python程序.我无法显示多个图像.下面是一个重现错误的小型完整程序.程序直接递归搜索当前的jpg文件,并在用户按Enter键时显示它们.
import Tkinter, ImageTk,os, re
def ls_rec(direc):
try:
ls = os.listdir(direc)
except Exception as e:
return
for f in os.listdir(direc):
fpath = os.path.join(direc, f)
if os.path.isfile(fpath):
yield fpath
elif os.path.isdir(fpath):
for f2 in iterate_dir(os.path.join(direc,f)):
yield f2
images = filter(lambda a:re.match('.*\\.jpg$',a),ls_rec(os.getcwd()))
assert(len(images)>10)
top = Tkinter.Tk()
image_label = Tkinter.Label(top)
Label_text = Tkinter.Label(top,text="Below is an image")
img = None
i = 0
def get_next_image(event = None):
global i, img
i+=1
img = ImageTk.PhotoImage(images[i])
label.config(image=img)
label.image = img
top.bind('<Enter>',get_next_image)
label.pack(side='bottom')
Label_text.pack(side='top')
get_next_image()
top.mainloop()
该程序失败并带有以下回溯:
Traceback (most recent call last):
File "/usr/lib/python2.7/pdb.py", line 1314, in main
pdb._runscript(mainpyfile)
File "/usr/lib/python2.7/pdb.py", line 1233, in _runscript
self.run(statement)
File "/usr/lib/python2.7/bdb.py", line 387, in run
exec cmd in globals, locals
File "<string>", line 1, in <module>
File "/home/myuser/Projects/sample_images.py", line 1, in <module>
import Tkinter, ImageTk,os, re
File "/home/myuser/Projects/sample_images.py", line 32, in get_next_image
img = ImageTk.PhotoImage(some_image[1])
File "/usr/lib/python2.7/dist-packages/PIL/ImageTk.py", line 109, in __init__
mode = Image.getmodebase(mode)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 245, in getmodebase
return ImageMode.getmode(mode).basemode
File "/usr/lib/python2.7/dist-packages/PIL/ImageMode.py", line 50, in getmode
return _modes[mode]
KeyError: '/home/myuser/sampleimage.jpg'
有没有人在运行此代码时获得相同的行为?我究竟做错了什么?
编辑:使用korylprince的解决方案,并进行一些清理,以下是原始代码的工作版本:
import os, re, Tkinter, ImageTk
def ls_rec(direc, filter_fun=lambda a:True):
for (dirname, dirnames, fnames) in os.walk(direc):
for fname in fnames:
if filter_fun(fname):
yield os.path.join(dirname,fname)
top = Tkinter.Tk()
image_label = Tkinter.Label(top)
text_label = Tkinter.Label(top,text="Below is an image")
images = ls_rec(os.getcwd(), lambda a:re.match('.*\\.jpg$',a))
imgL = []
def get_next_image(event = None):
fname = images.next()
print fname
fhandle = open(fname)
img = ImageTk.PhotoImage(file=fhandle)
fhandle.close()
imgL.append(img)
image_label.config(image=img)
top.bind('<Return>',get_next_image)
image_label.pack(side='bottom')
text_label.pack(side='top')
get_next_image()
top.mainloop()
编辑:top.bind(‘< Enter>‘…)实际上绑定了鼠标进入框架的事件,而不是用户按Enter键.正确的行是top.bind(‘< Return>‘,…).
解决方法:
ImageTk.PhotoImage没有真正正确记录.
你应该尝试这样的事情:
#outside of functions
images = list()
#inside function
global images
with open(images[i]) as f:
img = ImageTk.PhotoImage(file=f)
images.append(img)
将图像放在列表中的原因是python将引用它.否则垃圾收集器最终将删除图像对象.
内容总结
以上是互联网集市为您收集整理的python – 使用Tkinter显示图像全部内容,希望文章能够帮你解决python – 使用Tkinter显示图像所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。