(“AttributeError:“PhotoImage”对象没有属性“\u PhotoImage\u photo”)[Tkinter]

2024-04-29 11:33:07 发布

您现在位置:Python中文网/ 问答频道 /正文

代码:

from tkinter import*
from PIL import Image,ImageTk

class Register:
    def __init__(self,root):
        self.root = root
        self.root.title("Registration Window")
        self.root.geometry("1350x700+0+0")

        self.bg = ImageTk.PhotoImage(file="Images/waterfall.jpg")
        bg = Label(self.root,image = self.bg).place(x=250,y=0,relwidth=1,relheight=1)

root = Tk()*emphasized text*
obj = Register(root)
root.mainloop()

上面是一段代码,用于在python中创建注册表单并获得以下错误。在python中使用tkinter模块,我正在创建注册表单,这意味着当我在窗口self.bg = ImageTk.PhotoImage(file="Images/waterfall.jpg")中设置背景图像并出错时,我从联机获取图像,并将其设置为高度和宽度

错误:

Traceback (most recent call last):
  File "d:\Login with Database\register.py", line 16, in <module>
    obj = Register(root)
  File "d:\Login with Database\register.py", line 10, in __init__
    self.bg = ImageTk.PhotoImage(file="Images/waterfall.jpg")
  File "C:\Python\Python38\lib\site-packages\PIL\ImageTk.py", line 89, in __init__
    image = _get_image_from_kw(kw)
  File "C:\Python\Python38\lib\site-packages\PIL\ImageTk.py", line 58, in _get_image_from_kw
    return Image.open(source)
  File "C:\Python\Python38\lib\site-packages\PIL\Image.py", line 2878, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'Images/waterfall.jpg'
Exception ignored in: <function PhotoImage.__del__ at 0x000001928E23C430>
Traceback (most recent call last):
  File "C:\Python\Python38\lib\site-packages\PIL\ImageTk.py", line 118, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

Tags: infrompyselfpillinerootfile
1条回答
网友
1楼 · 发布于 2024-04-29 11:33:07

我不使用这个

self.bg = ImageTk.PhotoImage(file="Images/waterfall.jpg")
bg = Label(self.root,image = self.bg).place(x=250,y=0,relwidth=1,relheight=1)

试试这个

self.bg=ImageTk.PhotoImage(file="Images/waterfall.jpg")
self.bg_image=Label(self.root, image=self.bg).place(x=0, y=0, relwidth=1, relheight=1)

另外,请仔细检查瀑布.jpg所在的文件

相关问题 更多 >