Python,Tkinter,PIL,Mac显示图像的问题



我已经在stackoverflow上搜索了一段时间,想找到答案,但没有任何运气!我在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()

改为使用这个:

    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()

改为使用此从PIL,导入图像,ImageTk然后你的图像不能显示在一个框架中,使用标签代替

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

最新更新