2012-09-25 3 views
0

웹 캠에서 비디오 피드를 가져 와서 Tkinter 창에 표시하는 프로그램을 작성 중입니다. 우분투 12.04에서 실행 한 다음 코드를 작성했습니다.Gstreamer 웹캠 비디오 피드 in tkinter

#!/usr/bin/env python 

import sys, os, gobject 
from Tkinter import * 
import pygst 
pygst.require("0.10") 
import gst 

# Goto GUI Class 
class Prototype(Frame): 
    def __init__(self, parent): 
     gobject.threads_init() 
     Frame.__init__(self, parent)  

     # Parent Object 
     self.parent = parent 
     self.parent.title("WebCam") 
     self.parent.geometry("640x560+0+0") 
     self.parent.resizable(width=FALSE, height=FALSE) 

     # Video Box 
     self.movie_window = Canvas(self, width=640, height=480, bg="black") 
     self.movie_window.pack(side=TOP, expand=YES, fill=BOTH) 

     # Buttons Box 
     self.ButtonBox = Frame(self, relief=RAISED, borderwidth=1) 
     self.ButtonBox.pack(side=BOTTOM, expand=YES, fill=BOTH) 

     self.closeButton = Button(self.ButtonBox, text="Close", command=self.quit) 
     self.closeButton.pack(side=RIGHT, padx=5, pady=5) 

     gotoButton = Button(self.ButtonBox, text="Start", command=self.start_stop) 
     gotoButton.pack(side=RIGHT, padx=5, pady=5) 

     # Set up the gstreamer pipeline 
     self.player = gst.parse_launch ("v4l2src ! video/x-raw-yuv,width=640,height=480 ! ffmpegcolorspace ! xvimagesink") 

     bus = self.player.get_bus() 
     bus.add_signal_watch() 
     bus.enable_sync_message_emission() 
     bus.connect("message", self.on_message) 
     bus.connect("sync-message::element", self.on_sync_message) 

    def start_stop(self): 
     if self.gotoButton["text"] == "Start": 
      self.gotoButton["text"] = "Stop" 
      self.player.set_state(gst.STATE_PLAYING) 
     else: 
      self.player.set_state(gst.STATE_NULL) 
      self.gotoButton["text"] = "Start" 

    def on_message(self, bus, message): 
     t = message.type 
     if t == gst.MESSAGE_EOS: 
      self.player.set_state(gst.STATE_NULL) 
      self.button.set_label("Start") 
     elif t == gst.MESSAGE_ERROR: 
      err, debug = message.parse_error() 
      print "Error: %s" % err, debug 
      self.player.set_state(gst.STATE_NULL) 
      self.button.set_label("Start") 

    def on_sync_message(self, bus, message): 
     if message.structure is None: 
      return 
     message_name = message.structure.get_name() 
     if message_name == "prepare-xwindow-id": 
      # Assign the viewport 
      imagesink = message.src 
      imagesink.set_property("force-aspect-ratio", True) 
      imagesink.set_xwindow_id(self.movie_window.window.xid) 

def main(): 
    root = Tk() 
    app = Prototype(root) 
    app.pack(expand=YES, fill=BOTH) 
    root.mainloop() 


if __name__ == '__main__': 
    main() 

프로그램이 실행 중일 때 출력 창에 내 문제는 ButtonBox도 VideoBox도 표시되지 않습니다. 이 문제를 어떻게 해결할 수 있습니까? 가능한 솔루션 (예 : http://pygstdocs.berlios.de/#projects 또는 Way to play video files in Tkinter?)이있는 다른 사이트를 살펴 보았지만 코드의 의미에 대한 정보는 매우 제한적이었습니다.

버튼을 작동시키기 위해 제안 된 변경과 몇 가지 다른 작업을 수행 한 후에 프로그램을 실행할 때 디스플레이 창과 기본 창과의 차이점을 알았습니다. tkinter를 사용할 때 비디오를 메인 윈도우에 표시하는 방법이 있습니까 ??

답변

2

Prototype 클래스가 Tkinter 프레임 인 것처럼 보이지만 어디에도 포장하거나 배치하지 않은 것처럼 보입니다.

... 
app = Prototype(root) 
app.pack(expand=YES, fill=BOTH) 
root.mainloop() 
+0

도움을 주셔서 감사합니다. :) – user1696565

1

마지막으로 질문에 대한 해결책을 찾았습니다. 나는 오류가 라인 imagesink.set_xwindow_id에 내가 imagesink.set_xwindow_id로 변경

(self.movie_window.window.xid) 것을 깨달았다 (self.movie_window.winfo_id())

는 실수입니다 gtk 위젯의 속성 인 window.xid를 사용했다. tkinter에서 winfo_id()는 tkinter 위젯의 창 식별자를 반환합니다. 더 많은 정보를 원하시면 http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.winfo_id-method

관련 문제