【问题标题】:Trying to upload a photo using Tkinter photoimage尝试使用 Tkinter photoimage 上传照片
【发布时间】:2016-08-22 08:47:05
【问题描述】:

我一直在尝试将照片加载到 Tkinter 画布上,但一直失败。我得到的消息是:TclError: couldn't open "C:\Jules\...\photo_files•0651442_51c04521d6.gif": no such file or directory.

我正在使用 Python 2.7。 这是我的代码。

import Tkinter
topthree = Tkinter.Tk()

canvas = Tkinter.Canvas(topthree, height=800, width= 800)
canvas.grid(row = 0, column = 0)
photo = Tkinter.PhotoImage(file = "C:\Jules\...\6250651442_51c04521d6.gif")
im = canvas.create_image(0,0, image=photo)

canvas.pack()
topthree.mainloop()

【问题讨论】:

    标签: python-2.7 tkinter tkinter-canvas


    【解决方案1】:

    在文件名前添加r

    photo = Tkinter.PhotoImage(file = r"C:\Jules\...\6250651442_51c04521d6.gif")
    

    这是因为反斜杠将下一个字符解释为控制字符,这会导致一些字符消失。例如,\n 变为新行,n 消失...

    在字符串取消控制字符之前写入r。 (r 代表原始字符串)

    【讨论】:

    • 更好:使用正斜杠而不是反斜杠。
    猜你喜欢
    • 2017-07-22
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2017-02-02
    • 2011-11-05
    • 2013-07-19
    相关资源
    最近更新 更多