Tkinter在第二个窗口中显示网络摄像头视图
问题描述:
我正在使用网络摄像机视图并对拍摄的图像执行分析。我想介绍一种功能,其中可以调用窗口并且用户可以在新窗口中查看网络摄像头视图,他们是否愿意。不过,当我打开新窗口时,我的主要窗口中的按钮会切换到实例。出了什么问题?Tkinter在第二个窗口中显示网络摄像头视图
这里是我的(工作)例如:
import Tkinter as tk
import cv2
from PIL import Image, ImageTk
class CamView():
def __init__(self, parent):
self.parent = parent
self.window = tk.Toplevel(parent)
self.window.protocol("WM_DELETE_WINDOW", self.close)
self.show_frame()
def show_frame(self):
imgtk = ImageTk.PhotoImage(image=self.parent.img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
def close(self):
self.parent.test_frame = None
self.window.destroy()
root = tk.Tk()
root.bind('<Escape>', lambda e: root.quit())
lmain = tk.Label(root)
lmain.pack()
class Main(tk.Frame):
def __init__(self, parent):
self.test_frame = None
frame = tk.Frame.__init__(self,parent)
a = tk.Label(text='hello!').pack()
b = tk.Button(frame, text='open', command=self.load_window)
b.pack()
width, height = 800, 600
self.cap = cv2.VideoCapture(0)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
self.do_stuff()
def do_stuff(self):
_, frame = self.cap.read()
frame = cv2.flip(frame, 1)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
self.img = Image.fromarray(cv2image)
if self.test_frame != None:
self.test_frame.show_frame()
lmain.after(10, self.do_stuff)
def load_window(self):
self.test_frame = CamView(self)
control = Main(root)
root.mainloop()
在我真正的代码,以及该工作示例 - 看来,当我打开新的窗口,它把网络摄像头帧的第一个窗口当我不希望它!
答
固定!我因为self.lmain
而感到困惑。这里是工作代码:
import Tkinter as tk
import cv2
from PIL import Image, ImageTk
class CamView():
def __init__(self, parent):
self.parent = parent
self.window = tk.Toplevel(parent)
self.lmain2 = tk.Label(self.window)
self.lmain2.pack()
self.window.protocol("WM_DELETE_WINDOW", self.close)
self.show_frame()
def show_frame(self):
imgtk = ImageTk.PhotoImage(image=self.parent.img)
self.lmain2.imgtk = imgtk
self.lmain2.configure(image=imgtk)
def close(self):
self.parent.test_frame = None
self.window.destroy()
root = tk.Tk()
root.bind('<Escape>', lambda e: root.quit())
class Main(tk.Frame):
def __init__(self, parent):
self.lmain = tk.Label(parent)
self.lmain.pack()
self.test_frame = None
frame = tk.Frame.__init__(self,parent)
a = tk.Label(text='hello!').pack()
b = tk.Button(frame, text='open', command=self.load_window)
b.pack()
width, height = 800, 600
self.cap = cv2.VideoCapture(0)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
self.do_stuff()
def do_stuff(self):
_, frame = self.cap.read()
frame = cv2.flip(frame, 1)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
self.img = Image.fromarray(cv2image)
if self.test_frame != None:
self.test_frame.show_frame()
self.lmain.after(10, self.do_stuff)
def load_window(self):
if self.test_frame == None:
self.test_frame = CamView(self)
control = Main(root)
root.mainloop()
我没有相机或cv2,所以我不能运行你的代码或提供很多帮助这个神秘的问题,不幸的是。但是,您应该使用'class CamView(object):'使'CamView'成为一种新式类。这种改变可能不会影响你的问题,但使用旧式类很少是一个好主意。另外,'self.test_frame不是None'优于'self.test_frame!= None'。 –
感谢您的建议! – Sam