0

for a personnal project I've been playing with pyvirtualcam, and wanted a nice feedback with interactions, so I started implementing some tkinter in it. But as I tried to display the image on the canvas, I faced a wall...

My problem is that when printing that "image" which I forced to be a black square, it do not display at all (I am convinced about this because my canvas background is red). Any idea where this can come from?

Here is the code to reproduce the problem I have:

        self.screen = tk.Tk()
        self.canvas = tk.Canvas(self.screen, bg = "red")
        self.canvas.pack(fill=tk.BOTH, expand=tk.YES, side=tk.TOP)
        size = 20, 20, 3
        output = np.zeros(size, dtype=np.uint8)
        imageRGB = cv2.cvtColor(output, cv2.COLOR_BGRA2RGBA)
        imageTk = ImageTk.PhotoImage(image = Image.fromarray(imageRGB))
        self.canvas.create_image(100, 100, image = imageTk, anchor=tk.NW)

questions you might ask:

  • console is empty as if everything was fine (no error)
  • when printing the "output" or "imageRGB" variables it prints the matrix fine,
  • when printing the "imageTk" variable is prints a < TkImage > object,

For more info, be free to ask me, Thanks for your help!

Ren
  • 157
  • 12
  • Try adding `self.canvas.tk_img = imageTk` at the end of that code. If my solution works read [this](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – TheLizzard Apr 24 '21 at 17:16
  • yeah It works ! thx a lot,I've read the other page and now get why – Ren Apr 24 '21 at 17:28
  • Also add `master=self.screen` to `ImageTk.PhotoImage(image=...)`. I am guessing that you have more than 1 instance of `tk.Tk()` so you need it – TheLizzard Apr 24 '21 at 17:31
  • I do only have 1 in fact, that s a only a little project and not a big one, but thanks for the advice! I'll remember it for more advenced projects! – Ren Apr 24 '21 at 17:34

0 Answers0