Python 3: Tkinter-background image does not exist, "pyimage1" doesn't exist
My Code to display a background image:
from tkinter import *
def entrar():
#Janela Principal
janela = Tk()
janela.title("Salvadados")
janela.geometry('400x600')
#fotofundo
back = Label(janela)
back.la = PhotoImage(file = 'C:/Users/Ivan/Videos/fundim2.gif')
back['image'] = back.la
back.place(x=0,y=0)
The following error is displayed:
**File "C:\Users\Ivan\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1473, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist**
Note: The enter() function is a command for a button to open this "saved"window.
2
1 answers
Only the code you showed works perfectly. Certainly, there is much more code from your program that you did not show.
Most of the time, this error occurs when you create one or more windows with Tk
or when you destroy the current one, create a new one and reuse the previous PhotoImage. Example:
This code generates the same error you showed because 2 windows are created:
from tkinter import *
def entrar():
#Janela Principal
janela = Tk()
janela.title("Salvadados")
janela.geometry('400x600')
#fotofundo
back = Label(janela)
back.la = PhotoImage(file = 'image.png')
back['image'] = back.la
back.place(x=0,y=0)
root = Tk()
entrar()
This code generates error as I try to reuse the Previous PhotoImage:
from tkinter import *
def entrar():
#Janela Principal
janela = Tk()
janela.title("Salvadados")
janela.geometry('400x600')
#fotofundo
back = Label(janela)
back.la = PhotoImage(file = 'C:/Users/Jean Extreme/Desktop/Jean Extreme/Extreme/Fotos/Placa Minecraft Like.png')
back['image'] = back.la
back.place(x=0,y=0)
return janela,back
root , back = entrar()
root.destroy()
new_root = Tk()
label = Label(new_root)
label["image"] = back.la
label.pack()
This code does not generate error:
from tkinter import *
def entrar():
#Janela Principal
janela = Tk()
janela.title("Salvadados")
janela.geometry('400x600')
#fotofundo
back = Label(janela)
back.la = PhotoImage(file = 'image.png')
back['image'] = back.la
back.place(x=0,y=0)
entrar()
To fix the problem you should not create more than one window with tkinter and whenever you destroy one window to create another, also create a new one PhotoImage
.
0
Author: JeanExtreme002, 2019-10-31 20:31:23