Inserting an image with tkinter

Solved
Bonjour,
I am trying to program the insertion of an image using tkinter but each time I get the error 'TclError: image "pyimage5" doesn't exist'. I would like to know what is wrong with my code.

class Interface(Tk): def __init__(self, path_image): super(Interface, self).__init__() self.image = PhotoImage(file=path_image) self.w, self.h = self.image.width(), self.image.height() self.canvas = Canvas(self, width=self.w, height=self.h) self.canvas.pack() self.canvas.create_image((self.w//2, self.h//2), image=self.image) self.mainloop() Interface(r"C:\Users\lbhad\OneDrive\Documents\BCPST\2\Informatique\logo2.png")


Configuration: Windows / Edge 87.0.664.75

4 answers

  1. Hello Neirdah,

    Why is the error pyimage5 does not exist when you pass logo2.png as an argument????????

    On another note, can you display your complete code?

    I tested your code with my own image, it works very well...
    0
    1. Because simply using 'logo2.png' the program cannot find the file.
      For now, this is my complete program, but I am trying to connect it to another one to have the image in the background (which I am currently failing to do since I am just starting with programming with tkinter).
      0
  2. I'm not simply telling you to put logo2.png, I'm just

    wondering why the error message says pyimage5

    does not exist

    I copied/pasted your code and replaced your path and name

    of the image with my path and an image of mine and it works perfectly.
    0
    1. Hello Neirdah,

      So, are you having success?
      I did this and it works:

       # -*- coding:Latin-1 -*- from tkinter import * class Interface(Tk): def __init__(self, path_image): super(Interface, self).__init__() self.image = PhotoImage(file=path_image) self.w, self.h = self.image.width()+10, self.image.height()+10 self.canvas = Canvas(self, width=self.w, height=self.h) self.canvas.pack() self.canvas.create_image(self.w//2, self.h//2, image=self.image) self.mainloop() Interface(r"C:\Phil\Dev\Python\tests\ccm\images\Fresque_01.png") 
      1
      1. This works now. I would like to know how to stretch the image to fit my entire frame. How can I make it adapt to its size?
        0