【问题标题】:Where do I store image that I am going to use in my python script?我在哪里存储要在我的 python 脚本中使用的图像?
【发布时间】:2013-07-13 21:12:14
【问题描述】:

我正在尝试在 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

抱歉,由于某种原因,这个网站不允许我编写该代码

附:我有一个mac

【问题讨论】:

  • 请在此处显示您尝试使用图像的代码。
  • 最干净的方法是将路径(例如image_dir = ...)存储在配置文件中并导入配置文件。阅读 Python 中的配置文件。

标签: python user-interface python-2.7 tkinter


【解决方案1】:

只要把它和你的脚本放在同一个文件里。

希望有帮助!

【讨论】:

    【解决方案2】:

    你可以使用绝对路径:

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

    或者更好的版本是将你的图片复制到这个位置:

    /Users/theroeloffs/Downloads/image.gif
    

    (您的脚本所在的位置)并且您只能使用文件名:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 2018-06-09
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多