我一直在尝试向我的界面添加背景图片,但总是收到错误消息:
"couldn't open "pyimage1": no such file or directory"
我也是python的新手。
已经尝试过使用tkinter和PIL以及tkinter的画布的多种方法,这些方法也不起作用
这是整个程序:
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()
主要问题可能在我猜测的这些行内:
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
如您所见,我正在尝试制作一个界面或GUI,除背景图片之外,其他所有东西都工作正常。
答案 0 :(得分:1)
尝试一下:
"skip.header.line.count"="1"
答案 1 :(得分:0)
据我所知,这仅意味着您在变量上使用了两次“ tk.PhotoImage”。
例如:
item1_image = tk.Label()
image = tk.PhotoImage('image.png')
item1_image.configure(image=tk.PhotoImage(image))
当将这些变量从大文件的不同位置拉出时,很难跟踪是否使用了“ PhotoImage”。我通常尽早使用它以避免图像不出现。