Python Forum
tkinter camera threads
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter camera threads
Hello everyone.
I have a big code in tkinter with 14 files.In one of my files I control my camera and show the output inside a frame (in tkinter).
So ,I will show you the code below ,it is implemented with threads. I want to use threads, because other parts of my program sticks a little bit.
But the problem is that the frame for output of camera is flashing a little bit.I don't know if my code is correct,
because I create and start a thread every time.
Using threads in video_stream(.) (see below) , other parts of program doesn't stick ,but my frame for the camera output is a little bit flashing.

Code if file
This is a function of (for example
import PIL , cv2
from PIL import Image , ImageTk
from tkinter import *
from datetime import datetime
import threading

cap = cv2.VideoCapture(2)

def video_stream(label_frame):
    ret , frame =
    if(not cap.isOpened() ):
        return -1
        frame = cv2.flip(frame , 1)
        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        img = PIL.Image.fromarray(cv2image)
        imgtk = ImageTk.PhotoImage(image = img)
        label_frame.imgtk = imgtk
        label_frame.configure(image = imgtk)
        thread_call_camera = threading.Thread(target=video_stream,args=(label_frame,))
        #label_frame.after(20 , video_stream , label_frame)
In another file ,for example ,I call: << value = video_stream(a frame I designed here) >>
I can't show you the whole code ,is big and maybe you will get confused.
I just want to tell me if I create threads normally.

Thanks a lot.
There are some examples of using threads with tkinter in the following link
I want to know if I implement the threads normal, because if you see, inside video_stream(.) ,thread calls the same function.
What do you think about it?
I know how to implement threads,but what about that case?
You will only get a return of -1 when it first travels through the function and the following evaluates to true
if(not cap.isOpened() ):
        return -1
otherwise, it travels right through the function and will return None.
Directly calling the GUI from inside another thread will cause problems.
Depending on how fast it travels through the function and starts another thread looping back on itself, it might be overwhelming the GUI with changes.
No,it doesn't go through the first if statement.It goes to elif and all works fine ,but as I said the frame for camera output is a little bit flashing.What do you suggest me to do?
Try removing the threading use the after call you have commented out instead.
Use another after call on label_frame.configure with a duration of 0 so it gets called in the GUI thread.
Hello again. I don't understand what you want me to do. Can you write it?
Thank you!

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter frame camera opencv Nick_tkinter 9 1,203 Mar-21-2021, 06:41 PM
Last Post: Nick_tkinter
  Raspberry Pi camera live feed in tkinter robgar2001 6 7,648 Sep-30-2018, 10:49 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020