__ tkinter.TclError:无法打开

问题描述 投票:0回答:1

一段时间以来,我一直在试图找出如何在tkinter上创建图像的方法,这是我的代码:

from tkinter import *
from PIL import ImageTk,Image
root  =Tk()
root.title("e.png")

canvas = Canvas(root, width = 500, height = 500)
canvas.pack
my_image = PhotoImage(file='C:\\Users\\david\\Desktop\\e.png')
canvas.create_image(0,0, anchor = NW, image=my_image)
root.mainloop()

因此,我收到此错误:

Traceback (most recent call last):
  File "/Users/david/Desktop/tinker.py", line 8, in <module>
    my_image = PhotoImage(file='C:\\Users\\david\\Desktop\\e.png')
  File     "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line     3542, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line     3498, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "C:\Users\david\Desktop\e.png": no such file or     directory

我已经确保\ Users \ david \ Desktop \ e.png和tkinker脚本一起在我的桌面上。我也尝试将它们放在同一文件夹中,但仍然收到相同的错误消息。

感谢您的帮助!

python tkinter
1个回答
0
投票

抱歉,4秒前我用过,

import tkinter as tk
from PIL import ImageTk,Image
root  =tk.Tk()
root.title("e.png")
photo = ImageTk.PhotoImage(Image.open('e.png'))
label = tk.Label(root, image=photo)
label.pack()

它终于奏效了。对不起,这很愚蠢,我很抱歉回答了我自己的问题。.我只是不小心将其编码,然后使它奏效了。。

© www.soinside.com 2019 - 2024. All rights reserved.