【问题标题】:Tkinter Image viewerTkinter 图像查看器
【发布时间】:2013-02-24 13:36:07
【问题描述】:

我正在尝试使用 Tkinter 创建一个程序,该程序在窗口中显示来自多个不同目录的缩略图。到目前为止,我有这个:

import Tkinter as tk
from PIL import Image, ImageTk
import Image, os

root = tk.Tk()
root.title('Shot Viewer')
w, h, x, y = 1000, 1000, 0, 0
root.geometry("%dx%d+%d+%d" % (w, h, x, y))

#quit
def quit(root):
    root.quit()
    root.destroy()

path = "/media/Expansion Drive/Heros Mission 3/Scenes/Scene 1-3/Shots/"
labels = []
for files in os.listdir(path):
    number = files.split("_")[1]
    filed = "/media/Expansion Drive/Heros Mission 3/Scenes/Scene 1-3/Shots/Shot_{} /Frames/Shot_{}_000000.png".format(number, number)
    if os.path.lexists(filed) == 'False':
        pass
    else:
        im = Image.open(imageFile)
        im.thumbnail((96, 170), Image.ANTIALIAS)
        image = ImageTk.PhotoImage(im)
        label = tk.Label(root, image=image, name=number)
        labels.append(label)

print labels

for label in labels:
    panel = label.grid()

panel2.grid(row=2, column=1)
button2 = tk.Button(panel2, text='Quit', command=lambda root=root:quit(root))
button2.grid(row=1, column=1, sticky='NW')

root.mainloop()

但是这不起作用,有人有什么建议吗?

谢谢 汤姆

【问题讨论】:

  • 错误说明了什么?
  • 请提供堆栈跟踪,或您认为代码无法正常工作的具体原因
  • 我们不知道错误是什么,我们无法提供帮助。我们不是读心者
  • 对不起,错误信息如下: Traceback (last recent call last): File "/home/tom/Desktop/Shot_viewer_SO.py", line 34, in panel2.grid(行=2,列=1)文件“/usr/lib/python2.7/lib-tk/Tkinter.py”,第1904行,在grid_configure + self._options(cnf,kw))TclError:无法调用“ grid”命令:应用程序已被销毁
  • 同时出现 tkinter 主窗口,但没有图像到位(或按钮)

标签: python image tkinter viewer


【解决方案1】:

使用 glob 模块帮助查找相关文件。

至于图片无法显示:

import Tkinter as tk
from PIL import Image, ImageTk
import glob

root = tk.Tk()

labels = []

for jpeg in glob.glob("C:/Users/Public/Pictures/Sample Pictures/*.jpg")[:5]:
    im = Image.open(jpeg)
    im.thumbnail((96, 170), Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(im)
    label = tk.Label(root, image=photo)
    label.pack()    
    label.img = photo # *
    # * Each time thru the loop, the name 'photo' has a different
    # photoimage assigned to it.
    # This means that you need to create a separate, 'longer-lived'
    # reference to each photoimage in order to prevent it from
    # being garbage collected.
    # Note that simply passing a photoimage to a Tkinter widget
    # is not enough to keep that photoimage alive.    
    labels.append(label)

root.mainloop()

【讨论】:

    【解决方案2】:

    我认为您在panels = label.grid() 的位置上没有正确处理它。相反,尝试只做label.grid,这样它就不是赋值运算符而是一个动作。

    【讨论】:

    • 从 label.grid 中删除括号确实会停止错误消息。但是,图像仍然无法出现在窗口中。我还必须删除按钮代码,因为这会引发此错误:init Widget.__init__ 中的文件“/usr/lib/python2.7/lib-tk/Tkinter.py”,第 2047 行(self, master, 'button', cnf, kw) File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1977, in init (widgetName, self. _w) + extra + self._options(cnf)) TclError:无法调用“按钮”命令:应用程序已被销毁(我无法输入完整的错误,因为没有足够的字符)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    相关资源
    最近更新 更多