[英]How can I put an image as a background on tkinter?
為了改變它,我想把藍色壁紙圖像作為我的 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.