1
votes

I want to simply display a logo or image in my GUI. I tried different techniques as posted on the internet, however all seems to give me an error as TclError: image "pyimage27" doesn't exist. I have checked the file is correct(png file) and the path too. I still cant display the image. Below is my simple code:

import tkinter as tk

root = tk.Tk()

def show_image():
    img_label = tk.Label(root)
    img_label.image = tk.PhotoImage(file="logo.png")
    img_label['image'] = img_label.image

    img_label.pack()


show_image()
root.mainloop()

Error that i get


  File "C:\Users\bb\Documents
\line.py", line 54, in <module>
    alien()

  File "C:\Users\bb\Documents
\test.py\line.py", line 26, in __init__
    self.alien2 = self.canvas.create_image((100,100),image=CardVar,anchor=CENTER)

  File "C:\Users\bb\Anaconda3\lib\tkinter\__init__.py", line 2489, in create_image
    return self._create('image', args, kw)

  File "C:\Users\bb\Anaconda3\lib\tkinter\__init__.py", line 2480, in _create
    *(args + self._options(cnf, kw))))

TclError: image "pyimage30" doesn't exist

Thank you

Many similar answers are available but that did not help :

  1. When using Tkinter, error: TclError: image "pyimage8" doesn't exist
  2. Python: Tkinter TclError: can't invoke "image" command
1
Does your simplified code actual generate the error when you run it? I suspect that your real code has more than one call to Tk(), this is one of the numerous problems that can result from that. - jasonharper
The error shown is from create_image() but your code does not have such statement. - acw1668
Yes , u r right. My main code has more calls to Tk() and it gives me the error. what is the solution? - codeheadache
Generally speaking, you should only call Tk() once. To avoid that depends only exactly what you're trying to do (which we can't tell from the sample code in your question). - martineau
You have to use Toplevel() to create additional windows. Tk() does much more than create a window - it creates the entire Tcl/Tk environment that implements the GUI, the window is just an unavoidable side-effect. If you call it more than once, you can have situations such as an image existing in one of the environments, and a widget wanting to use that image existing in another environment. - jasonharper

1 Answers

0
votes

You can try with PhotoImage(file="path", master=root)