【问题标题】:Python, Tkinter, PIL, Mac problems with displaying imagesPython、Tkinter、PIL、Mac 显示图像的问题
【发布时间】:2013-06-28 19:10:22
【问题描述】:

我现在在 * 上搜索了一段时间以找到答案,但没有任何运气!在我的 Mac 上使用 tkinterPIL 显示图像时遇到问题。他们只是无法在正在运行的应用程序中找到图像。这是我的 Python 课程的最后一部分,我真的需要让它完成课程。这是我收到的错误消息。

Traceback (most recent call last):
File "/Users/andrej/Documents/untitled", line 19, in <module>
imageFile = Image.open("Signori.png")

这是我的代码:

import Image
import ImageTk
import Tkinter

def new():
wind = Tkinter.Toplevel()
wind.geometry('600x600')
imageFile2 = Image.open("nesta.png")
image2 = ImageTk.PhotoImage(imageFile2)

panel2 = Tkinter.Label(wind , image=image2)
panel2.place(relx=0.0, rely=0.0)
wind.mainloop()

master = Tkinter.Tk()
master.geometry('600x600')
imageFile = Image.open("Signori.png")
image1 = ImageTk.PhotoImage(imageFile)

panel1 = Tkinter.Label(master , image=image1)
panel1.place(relx=0.0, rely=0.0)
B = Tkinter.Button(master, text = 'New image', command = new).pack()
master.mainloop()

【问题讨论】:

  • 您没有发布整个异常跟踪 - 引发的实际错误是什么?

标签: python-2.7 tkinter python-imaging-library


【解决方案1】:

改用这个:

    from PIL, import Images, ImageTk

如果您的图片无法在框架中显示,请改用标签:

    your_image = ImageTk.PhotoImage(image.open('an image.png')) 
    # note the image has to be in the same directory as your project, 
    # then 
    your_label = Label(master=root, image=your_image) 
    your_label.pack()

【讨论】:

    【解决方案2】:

    改用这个 从 PIL,导入图像,ImageTk 那么你的图像不能在框架中显示,请改用标签

    your_image = ImageTk.PhotoImage(image.open('an image.png')) # 注释 图像必须与您的项目位于同一目录中, #那么 your_label = 标签(master=root, image=your_image) your_label.pack()

    【讨论】:

    • 尝试使用带有“`”的代码块,让它们更具可读性