import cv2 import threading import tkinter as tk from PIL import Image, ImageTk class CameraThread(threading.Thread): def __init__(self, camera_index, label): threading.Thread.__init__(self) self.camera_index = camera_index self.label = label self.stop_event = threading.Event() def run(self): cap = cv2.VideoCapture(self.camera_index) while not self.stop_event.is_set(): ret, frame = cap.read() if ret: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(frame) image = ImageTk.PhotoImage(image) self.label.config(image=image) self.label.image = image cap.release() def stop(self): self.stop_event.set() def main(): root = tk.Tk() root.title("Multiple Camera Display") camera_indices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] labels = [] threads = [] for i, camera_index in enumerate(camera_indices): label = tk.Label(root) label.grid(row=i // 4, column=i % 4) labels.append(label) thread = CameraThread(camera_index, label) thread.start() threads.append(thread) root.mainloop() print("结束窗口") for thread in threads: thread.stop() thread.join() if __name__ == "__main__": main()
标签:__,index,Tkinter,self,label,camera,多线程,image From: https://www.cnblogs.com/zwnsyw/p/17796566.html