使用Tkinter python2的标签中的实时流相机

时间:2019-05-17 15:12:29

标签: python tkinter tk

我想在tcinter和opencv和python2上使用标签绑定实时摄像机,但问题是当我单击按钮时,标签中仅显示一帧。

#! / usr / bin / env python         #--编码:utf-8--         #         #PAGE版本4.22生成的GUI模块         #与Tcl版本8.6结合使用         #五月17,2019 06:44:40 PM PKT平台:Linux


    import sys

    try:
        import Tkinter as tk
    except ImportError:
        import tkinter as tk

    try:
        import ttk
        py3 = False
    except ImportError:
        import tkinter.ttk as ttk
        py3 = True

    import gui_support

    from Tkinter import *
    from PIL import ImageTk, Image
    import cv2




    def vp_start_gui():
        '''Starting point when module is the main routine.'''
        global val, w, root
        root = tk.Tk()
        top = Toplevel1 (root)
        gui_support.init(root, top)
        root.mainloop()

    w = None
    def create_Toplevel1(root, *args, **kwargs):
        '''Starting point when module is imported by another program.'''
        global w, w_win, rt
        rt = root
        w = tk.Toplevel (root)
        top = Toplevel1 (w)
        gui_support.init(w, top, *args, **kwargs)
        return (w, top)

    def destroy_Toplevel1():
        global w
        w.destroy()
        w = None

    class Toplevel1:
        def __init__(self, top=None):
            '''This class configures and populates the toplevel window.
               top is the toplevel containing window.'''
            _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
            _fgcolor = '#000000'  # X11 color: 'black'
            _compcolor = '#d9d9d9' # X11 color: 'gray85'
            _ana1color = '#d9d9d9' # X11 color: 'gray85'
            _ana2color = '#ececec' # Closest X11 color: 'gray92'
            self.style = ttk.Style()
            if sys.platform == "win32":
                self.style.theme_use('winnative')
            self.style.configure('.',background=_bgcolor)
            self.style.configure('.',foreground=_fgcolor)
            self.style.configure('.',font="TkDefaultFont")
            self.style.map('.',background=
                [('selected', _compcolor), ('active',_ana2color)])

            top.geometry("1308x647+69+51")
            top.title("New Toplevel")

            self.menubar = tk.Menu(top,font="TkMenuFont",bg=_bgcolor,fg=_fgcolor)
            top.configure(menu = self.menubar)

            self.Label1 = tk.Label(top)
            self.Label1.place(relx=0.268, rely=0.015, height=31, width=599)
            self.Label1.configure(font="-family {Bitstream Vera Sans} -size 21 -weight bold")
            self.Label1.configure(text='''Face ID Attendance Marking System''')
            self.Label1.configure(width=599)

            self.TSeparator1 = ttk.Separator(top)
            self.TSeparator1.place(relx=0.245, rely=0.077, relwidth=0.497)

            self.TPanedwindow1 = ttk.Panedwindow(top, orient="horizontal")
            self.TPanedwindow1.place(relx=0.008, rely=0.139, relheight=0.68
                    , relwidth=0.982)
            self.TPanedwindow1.configure(width=1285)
            self.TPanedwindow1_p1 = ttk.Labelframe(width=650, text='Pane 1')
            self.TPanedwindow1.add(self.TPanedwindow1_p1)
            self.TPanedwindow1_p2 = ttk.Labelframe(text='Pane 2')
            self.TPanedwindow1.add(self.TPanedwindow1_p2)
            self.__funcid0 = self.TPanedwindow1.bind('<Map>', self.__adjust_sash0)

            self.Label2 = tk.Label(self.TPanedwindow1_p1)
            self.Label2.place(relx=0.016, rely=0.045, height=411, width=619
                    , bordermode='ignore')
            self.Label2.configure(text='''Label''')
            self.Label2.configure(width=619)

            self.TButton1 = ttk.Button(top)
            self.TButton1.place(relx=0.031, rely=0.835, height=28, width=83)
            self.TButton1.configure(takefocus="")
            self.TButton1.configure(text='''Tbutton''')
            self.TButton1.configure(command = self.cameratk)

        def __adjust_sash0(self, event):
            paned = event.widget
            pos = [640, ]
            i = 0
            for sash in pos:
                paned.sashpos(i, sash)
                i += 1
            paned.unbind('<map>', self.__funcid0)
            del self.__funcid0

        def cameratk(self):

            cap = cv2.VideoCapture(0)

            _, frame = cap.read()
            cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
            img = Image.fromarray(cv2image)
            imgtk = ImageTk.PhotoImage(image=img)
            self.Label2.imgtk = imgtk
            self.Label2.configure(image=imgtk)
            self.Label2.after(1, video_stream)



    if __name__ == '__main__':
        vp_start_gui()

1 个答案:

答案 0 :(得分:0)

已解决

self.Label2.after(1, video_stream)

Video_Stream被函数名称替换为self.cameratk

self.Label2.after(1, self.cameratk)

cap = cv2.VideoCapture(0)在函数外部。