简体   繁体   English

尝试打开图像时,T​​kinter“无法打开“pyimage1”:没有这样的文件或目录”错误

[英]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.

相关问题 tkinter matplotlib 错误_tkinter.TclError:图像“pyimage1”在 Pycharm 中不存在 - tkinter matplotlib error _tkinter.TclError: image "pyimage1" doesn't exist in Pycharm Tkinter 说 pyimage1 不存在,即使它在尝试将其放在 label 上时也是如此 - Tkinter saying pyimage1 doesn't exist even though it does when trying to put it on a label `_tkinter.TclError:图像“pyimage1”不存在` - `_tkinter.TclError: image “pyimage1” doesn't exist` _tkinter.TclError:图像“pyimage1”不存在 - _tkinter.TclError: image “pyimage1” doesn't exist 当我尝试使用 tkinter 将图像加载到 canvas 时,是什么导致错误“未知选项“pyimage1”'? - What causes the error 'unknown option “pyimage1”' while I'm trying to load image onto canvas with tkinter? Tkinter OOP“ PyImage1”不存在,错误 - Tkinter OOP “PyImage1” doesn't exist, error 使用 tkinter 按钮在海龟中打开图像时出现错误“pyimage1”不存在 - I am having an error 'pyimage1' doesn't exist while opening image in turtle with tkinter button 图像“pyimage1”不存在 - image "pyimage1" doesn't exist Python Tkinter:_tkinter.TclError:图像“pyimage1”不存在 - Python Tkinter: _tkinter.TclError: image "pyimage1" doesn't exist _tkinter.TclError:图像“pyimage1”不存在,我是python新手 - _tkinter.TclError: image "pyimage1" doesn't exist, I am new to python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM