【问题标题】:Tkinter Show webcam view in second windowTkinter 在第二个窗口中显示网络摄像头视图
【发布时间】:2016-12-02 16:56:22
【问题描述】:

我正在使用网络摄像头视图并对拍摄的图像进行分析。我希望引入一个功能,可以调用一个窗口,并且用户可以根据需要在新窗口中查看网络摄像头视图。但是,当我打开新窗口时,我的尝试导致主窗口中的按钮切换到实例。怎么了?

这是我的(工作)示例:

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()

在我的真实代码以及这个工作示例中 - 似乎当我加载新窗口时,它会在我不希望它时将网络摄像头框架放置在第一个窗口中!

【问题讨论】:

  • 很遗憾,我没有相机或 cv2,因此无法运行您的代码或对这个神秘问题提供太多帮助。但是,您应该使用class CamView(object): 使CamView 成为一种新型类。此更改可能不会影响您的问题,但使用旧式类很少是一个好主意。此外,self.test_frame is not None 优于 self.test_frame != None
  • 感谢您的建议!

标签: python opencv tkinter


【解决方案1】:

已修复!因为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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多