class Camera(): def __init__(self, top): self.top = top self.h = int(data_demo.window_height // 1.5) self.w = int(data_demo.window_width // 1.74) self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=self.w, height=self.h, highlightthickness=2, highlightbackground='Black') self.canvas2.place(relx=0.0, rely=0.032) label = tk.Label(self.canvas2, text='摄像头显示区!', font=("黑体", 25), width=15, height=1) label.place(relx=0.32, rely=0.47, anchor='nw') def Thread_(self): self.bool = True self.cap = cv2.VideoCapture(0) t = threading.Thread(target=self.windows_display()) t.start() def windows_display(self): self.canvas2.delete('all') while self.bool: if data_demo.display_id is True: ref, frame = self.cap.read() image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image1 = Image.fromarray(image) image2 = image1.resize((self.w, self.h), Image.ANTIALIAS) self.photo = ImageTk.PhotoImage(image=image2) self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW) self.canvas2.update() self.canvas2.after(15, self.windows_display())这个为什么会报错
时间: 2024-02-14 10:18:03 浏览: 25
这段代码中,`threading.Thread()` 方法的 `target` 参数应该传递一个函数名,而不是一个函数的返回值,因此需要将 `self.windows_display()` 修改为 `self.windows_display`。
此外,在 `windows_display()` 方法的最后,应该调用 `after()` 方法来实现定时更新画面,而不是在方法内部递归调用自身。递归调用会导致栈溢出,程序崩溃。
修改后的代码如下:
```python
class Camera():
def __init__(self, top):
self.top = top
self.h = int(data_demo.window_height // 1.5)
self.w = int(data_demo.window_width // 1.74)
self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=self.w, height=self.h,
highlightthickness=2, highlightbackground='Black')
self.canvas2.place(relx=0.0, rely=0.032)
label = tk.Label(self.canvas2, text='摄像头显示区!', font=("黑体", 25), width=15, height=1)
label.place(relx=0.32, rely=0.47, anchor='nw')
def Thread_(self):
self.bool = True
self.cap = cv2.VideoCapture(0)
t = threading.Thread(target=self.windows_display)
t.start()
def windows_display(self):
self.canvas2.delete('all')
while self.bool:
if data_demo.display_id is True:
ref, frame = self.cap.read()
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image1 = Image.fromarray(image)
image2 = image1.resize((self.w, self.h), Image.ANTIALIAS)
self.photo = ImageTk.PhotoImage(image=image2)
self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW)
self.canvas2.update()
self.canvas2.after(15)
```