[英]Tkinter "couldn't open "pyimage1": no such file or directory" error when trying to open an image
I have been trying to add a background image to my Interface but I always get the error:我一直在尝试向我的界面添加背景图像,但我总是收到错误消息:
"couldn't open "pyimage1": no such file or directory"
Also I am pretty new to python.另外我对python很陌生。
Already tried multiple methods with tkinter and PIL aswell as tkinter's canvas which didn't work either已经使用 tkinter 和 PIL 以及 tkinter 的画布尝试了多种方法,但也不起作用
This is the whole programm:这是整个程序:
import tkinter as tk
from PIL import Image, ImageTk
class MainMenu:
def __init__(self, master):
#creating Main Frame and Window
master.title("Interface")
#Image
image = Image.open(r"Images\matrix.jpg")
photo = ImageTk.PhotoImage(image)
self.background_image = tk.PhotoImage(file=photo)
self.background_label = tk.Label(image=self.background_image)
self.background_label.place(x=0,y=0)
self.background_label.img = self.background_image
#Creating Widgets
self.label1 = tk.Label(master, text="Please Enter the text you would
like encrypted: ")
self.entry1 = tk.Text(master, height=5, width=20)
self.button = tk.Button(master, text="Submit", command=self.Submit)
#Adding Widgets to Grid
self.label1.grid(row=0, column=0, padx=5, pady=5)
self.entry1.grid(row=1, column=0, padx=5, pady=5)
self.button.grid(columnspan=2, pady=10)
#Configuration of Widgets and Main window
master.configure(bg="black")
self.button.configure(bg="light blue")
self.label1.configure(bg="black", fg="light blue")
self.entry1.configure(bg="light blue")
def Submit(self):
print("You entered: " + self.entry1.get())
root = tk.Tk()
Mm = MainMenu(root)
root.mainloop()
The main problem would be within these lines I am guessing:主要问题将在我猜测的这些行内:
image = Image.open(r"Images\matrix.jpg")
photo = ImageTk.PhotoImage(image)
self.background_image = tk.PhotoImage(file=photo)
self.background_label = tk.Label(image=self.background_image)
self.background_label.place(x=0,y=0)
self.background_label.img = self.background_image
As you can see I am trying to make an Interface or GUI and everything is working fine except the background image.如您所见,我正在尝试制作界面或 GUI,除了背景图像外,一切正常。
Try this:尝试这个:
image = Image.open("Images\matrix.jpg")
photo = ImageTk.PhotoImage(image)
#self.background_image = tk.PhotoImage(file=photo) -- Not needed, delete
self.background_label = tk.Label(image=photo)
self.background_label.image = photo
self.background_label.place(x=0,y=0)
#self.background_label.img = self.background_image -- Also not needed, delete
As far as I can tell this simply means you have used 'tk.PhotoImage' twice on a variable.据我所知,这只是意味着您在一个变量上使用了两次 'tk.PhotoImage'。
For example:例如:
item1_image = tk.Label()
image = tk.PhotoImage('image.png')
item1_image.configure(image=tk.PhotoImage(image))
When you're pulling these variables out of different places in a large file, it is hard to keep track of whether 'PhotoImage' is used.当您从大文件的不同位置提取这些变量时,很难跟踪是否使用了“PhotoImage”。 I typically use it as early as possible to avoid the image not appearing.
我通常尽早使用它以避免图像不出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.