我在哪里存储我将在我的python脚本中使用的图像?

时间:2013-07-13 21:12:15

标签: python user-interface python-2.7 tkinter

我正在尝试在tkinter中渲染图像。我已经看到很多关于如何制作图像的堆栈溢出的脚本,但是当我尝试它说我的图片不存在时。是否有某个地方我需要存储我的图像?我的python脚本保存到桌面,但我知道python可能不会在那里搜索。我在哪里放置要在程序中显示的图像?提前谢谢!

编辑:这是请求的代码:

canvas = Canvas(app, width=300, height=250).pack()
picture = PhotoImage(file='image.gif') #image located in desktop
canvas.create_image(0,0, image = picture

这是错误:

Traceback (most recent call last):
    File "/Users/theroeloffs/Downloads/myprogram.py", line 87, in <module>
        piper_pic=PhotoImage(file = 'image.gif')
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 3306, in __init__
        Image.__init__(self, 'photo', name, cnf, master, **kw)
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 3262, in __init__
        self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't open "image.gif": no such file or directory
抱歉,由于某些原因,这个网站不允许我制作该代码

P.S。我有一个mac

2 个答案:

答案 0 :(得分:1)

您可以使用绝对路径:

picture = PhotoImage(file='/Users/theroeloffs/Desktop/image.gif')

或者更好的版本是,将图像复制到此位置:

/Users/theroeloffs/Downloads/image.gif

(你的脚本在哪里)而且你只能使用文件名:

picture = PhotoImage(file='image.gif')

答案 1 :(得分:1)

只需将其放在与脚本相同的文件中即可。

希望有所帮助!