I designed a window with tkinter and wanted to show a video stream there, and I also wanted to be able to pause the video with the "p" key. I created a function which puts a picture captured with a webcam into a label on the window. Then I put the function into the while loop to call it repeatedly and make a continuous video in the window. And I used waitKey() functions in the loop to read the key presses. The waitKey() also sets the frame rate or speed of the video. I managed to get the video running but the program does not react to key presses. On the other hand, I can change the frame rate when I change the argument of the waitKey(), so the function seems to work but it doesn't read the key presses and nothing happens when a key is pressed. There are also no error messages. I would be thankful if somebody would show me how to use the waitKey() in this loop so that I could control the video stream with key presses, or suggest a different way to do it. This is my code, most of it is designing the window but the loop with waitKey() functions is at the end:
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter.ttk import *
from PIL import ImageTk, Image
from cv2 import cv2
mainwin = Tk() #create main window
appWinWidth = int(0.6 * mainwin.winfo_screenwidth()) #set main window size (depending on the screen)
appWinHeight = int(0.5 * mainwin.winfo_screenheight())
screen_width = mainwin.winfo_screenwidth() #get screen dimensions
screen_height = mainwin.winfo_screenheight()
appWinX = int((screen_width - appWinWidth)/2) #set coordinates of the main window so that it
appWinY = int((screen_height - appWinHeight)/2) #would be located in the middle of the screen
#create and place the frames
frame1 = tk.Frame(mainwin, background = "white", width = int(appWinWidth/2), height = appWinHeight)
frame2 = tk.Frame(mainwin, background = "black", width = int(appWinWidth/2), height = appWinHeight)
frame1.grid(row = 0, column = 0, sticky = "nsew")
frame2.grid(row = 0, column = 1, sticky = "nsew")
mainwin.grid_columnconfigure(0, weight = 1)
mainwin.grid_columnconfigure(1, weight = 1)
mainwin.grid_rowconfigure(0, weight = 1)
#set the geometry of the main window
mainwin.geometry("{}x{}+{}+{}".format(appWinWidth, appWinHeight, appWinX, appWinY))
mainwin.resizable(width = 0, height = 0)
#create labels in frames
frame2.grid_propagate(0) #labels and other widgets in frame don't change the size of the frame
frame2.grid()
labelF2 = Label(frame2)
labelF2.grid()
labelF2.place(relx=0.5, rely=0.5, anchor="center")
frame1.grid_propagate(0)
labelF1 = Label(frame1, background = "white")
labelF1.grid()
mainwin.update()
#get camera feed and frame size
cap = cv2.VideoCapture(0)
capFrameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
capFrameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
widthRelation = capFrameWidth/frame2.winfo_width()
heigthRelation = capFrameHeight/frame2.winfo_height()
#define the size of the video frame so that the video would fit into the frame of the main window
if widthRelation > 1 and widthRelation > heigthRelation:
fittedSize = (int(capFrameWidth/widthRelation), int(capFrameHeight/widthRelation))
elif heigthRelation > 1 and heigthRelation > widthRelation:
fittedSize = (int(capFrameWidth/heigthRelation), int(capFrameHeight/heigthRelation))
else:
fittedSize = (capFrameWidth, capFrameHeight)
#funtion for getting a video frame, resizing it for the main window and placing it into the frame 2
def video_to_mainwin():
chk, frame = cap.read()
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
imgV = cv2.resize(cv2image, fittedSize)
img = Image.fromarray(imgV)
imgtk = ImageTk.PhotoImage(image=img)
labelF2.imgtk = imgtk
labelF2.configure(image=imgtk)
#run the video function continuously to create a stream, and be ready to react to key presses
while True:
key = cv2.waitKey(1)
if key == ord('p'): #if 'p' is pressed, pause the stream and write 'Paused' to frame 1 on the main window
video_to_mainwin()
labelF1.config(text = 'Paused')
mainwin.update()
key2 = cv2.waitKey(0) #remain paused until 'c' is pressed
if key2 == ord('c'):
labelF1.config(text = '')
else: #if no key is pressed, then just show the video stream
video_to_mainwin()
mainwin.update()
cv2.waitKey(1)
- usetkinter
for this -root.bind("p", function)
to runfunction
when you pressp
. And when you usebind()
then you don't needwhile True
to check pressed key -bind()
does it for you. Eventually you may needroot.after(time, function)
to runfunction
which update image in window.place()
,grid()
,pack()
are different 'layout managers` - every widget need only one layout manage - it makes no sense to dolabelF2.grid()
andlabelF2.place(...)