代码:
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模块,我创建了注册表,而当我在window 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'发布于 2021-06-28 02:45:08
我是客人,而不是用这个。
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)另外,仔细检查waterfall.jpg所在的文件。
https://stackoverflow.com/questions/63847614
复制相似问题