vivekkmr338
Posts: 6
Joined: Thu Jun 25, 2020 1:21 pm

invalid command name “1987269416show_frame” while executing “1987269416show_frame” (“after” script) in raspberry pi

Tue Jun 30, 2020 6:22 pm

Code: Select all

import tkinter as tk
import cv2
from PIL import Image, ImageTk


def show_frame():
    _, frame = cap.read()
    frame = cv2.flip(frame, 1)
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    lmain.after(10, show_frame)

width, height = 200, 600
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
root = tk.Tk()
lmain = tk.Label(root)
lmain.pack()
show_frame()
root.mainloop()
cap.release()

In windows the code is running ok but in raspberry pi it gives error also blur screen open in camera frame

User avatar
Paeryn
Posts: 2942
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: invalid command name “1987269416show_frame” while executing “1987269416show_frame” (“after” script) in raspberry pi

Tue Jun 30, 2020 11:50 pm

Usually that sort of error comes from the callback being called (or attempted to be called) after the underlying object has been deleted (that is the tcl/tk object, not python's). I'd expect that to happen the moment you close the window, the after is still pending and triggers. If that's the case then you should really save the id that after() returned to you and cancel it when you no longer need it.

Code: Select all

    show_frame_after_id = lmain.after(10, show_frame)
    
# somewhere in your code when lmain is going to be deleted
    lmain.after_cancel(show_frame_after_id)
10ms is quite a short time to be constantly calling that function, you're asking the image to be updated 100 times a second.
She who travels light — forgot something.

vivekkmr338
Posts: 6
Joined: Thu Jun 25, 2020 1:21 pm

Re: invalid command name “1987269416show_frame” while executing “1987269416show_frame” (“after” script) in raspberry pi

Wed Jul 01, 2020 7:41 am

Thanks for the reply, but i got video stream like the image i post.
Attachments
tk - Copy.png
tk - Copy.png (50.71 KiB) Viewed 95 times

vivekkmr338
Posts: 6
Joined: Thu Jun 25, 2020 1:21 pm

Re: invalid command name “1987269416show_frame” while executing “1987269416show_frame” (“after” script) in raspberry pi

Thu Jul 02, 2020 8:42 am

Problem solved after removing these two lines..

cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

Return to “Troubleshooting”