#๐Ÿ”’ tkinter refuses to draw my image on a canvas

11 messages ยท Page 1 of 1 (latest)

lament magnet
#

I have been really annoyed by this issue but it just isnt working. I am trying to put the camera feed on a tkinter canvas using cv2 but it just doesnt want to show
here is the code:

    canvas = tk.Canvas(root, width=vp.frame_size[0], height=vp.frame_size[1], background='gray75')

    cv_img = cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2RGB)
    photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(cv_img))

    # Add a PhotoImage to the Canvas
    canvas.create_image(0, 0, image=photo, anchor=tk.NW)
    canvas.pack(pady=20)

but it just shows the "gray75" background
any ideas?

hallow hearthBOT
#

@lament magnet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

elfin axle
lament magnet
#

yeah thats it

#

took too long

magic gorge
#

Really? Because the canvas should hold a reference now, should it not? I was going to suggest a call to canvas.update() at the end.

near estuary
shrewd ruin
#

Tkinter always has this kind of issues, in the past I found that using a list as a "cache" in the global scope solved issue with the references.

hallow hearthBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.