import cv2 import tkinter as tk from tkinter import * from PIL import Image, ImageTk#图像控件 from Layout_Settings.one import top1 class Two_Layout(): def __init__(self, top): self.top = top self.cap = cv2.VideoCapture(0) self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=1100, height=650, highlightthickness=2, highlightbackground='Black') self.canvas2.place(x=0, y=20) self.windows_display() def Camera(self): ref, frame = self.cap.read() if ref is True: frame = cv2.flip(frame, 1) # 摄像头翻转 cvimage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) pilImage = Image.fromarray(cvimage) pilImage = pilImage.resize((1100, 650), Image.ANTIALIAS) tkImage = ImageTk.PhotoImage(image=pilImage) return tkImage if ref is False: return False def windows_display(self): ref, frame = self.cap.read() if ref is False: label = tk.Label(self.canvas2, text='未接入摄像头!', font=("黑体", 25), width=15, height=1).place(x=400, y=300, anchor='nw') cap = cv2.VideoCapture(0) elif ref is not False: self.photo = ImageTk.PhotoImage(image=Image.fromarray(frame)) self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW) self.top.after(15, self.windows_display()) top = tk.Tk() Two_Layout(top)为什么不显示
时间: 2023-06-15 09:03:05 浏览: 112
code09_tkinter_login.pyw_python_tkinter_notepad.pyw_
可能是因为缺少必要的库或者代码逻辑有误。可以尝试检查一下以下几点:
1. 确定你已经安装了必要的库,比如 cv2,tkinter 和 PIL。
2. 确定你在代码中正确导入了库和模块,比如 from PIL import Image, ImageTk 以及 import tkinter as tk。
3. 检查代码逻辑是否有误,比如是否正确调用了 Camera 和 windows_display 函数。
如果以上都没问题,可以尝试添加一些 print 语句来调试程序,找到哪里出了问题。
阅读全文