Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将图像作为背景放在tkinter上?_Python_Python 3.x_Tkinter_Background Image - Fatal编程技术网

Python 如何将图像作为背景放在tkinter上?

Python 如何将图像作为背景放在tkinter上?,python,python-3.x,tkinter,background-image,Python,Python 3.x,Tkinter,Background Image,为了改变现状,我想在python脚本中添加蓝色墙纸图像作为背景。代码如下: from tkinter import * root = Tk() root.title("") root.iconbitmap() #root.minsize(width=1370, height=800) #root.maxsize(width=1370, height=800) background = PhotoImage(file="background.jpeg")

为了改变现状,我想在python脚本中添加蓝色墙纸图像作为背景。代码如下:

from tkinter import *

root = Tk()
root.title("")
root.iconbitmap()
#root.minsize(width=1370, height=800)
#root.maxsize(width=1370, height=800)

background = PhotoImage(file="background.jpeg")
background_label = Label(root,image=background)
background_label.place(x=0,y=0,relwidth=1,relheight=1)

root.mainloop()
但是,当我运行此代码时,会弹出以下错误:

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 4061, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 4006, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "background.jpeg"
有人知道如何解决这个问题吗?

您可以使用ImageTk:

from PIL import ImageTk
from tkinter import *

root = Tk()
root.title("")
root.iconbitmap()

background = ImageTk.PhotoImage(file="background.jpeg")
background_label = Label(root,image=background)
background_label.place(x=0,y=0,relwidth=1,relheight=1)

root.mainloop()

希望能有所帮助:D

这可能与此类似。这是否回答了您的问题?你能把我的答案标对吗