Python 3: Tkinter - Background image does not exist, "pyimage1" doesn’t exist

Asked

Viewed 835 times

2

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 that window "Save".

1 answer

0

Only the code you showed works perfectly. Certainly, there is much more code from your program that you have not shown.

Most of the time, this error occurs when you create one or more windows with the 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 causes error because 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.