# -*- coding: cp936 -*-
from Tkinter import *
import datetime
import time
import cv2
import numpy
root = Tk()
Python 与Opencv能否实现上位机界面视频与文本输入在同一窗口--尝试中
#root.title(unicode('与xxx聊天中','eucgb2312_cn'))
root.title("video")
#使用opencv自带的VideoCapture()函数定义摄像头对象,
#其参数0表示第一个摄像头,一般就是笔记本的内建摄像头
cap = cv2.VideoCapture(0)
#发送按钮事件
def sendmessage():
#在聊天内容上方加一行 显示发送人及发送时间
msgcontent = unicode('我:','eucgb2312_cn') + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) + '\n '
text_msglist.insert(END, msgcontent, 'green')
text_msglist.insert(END, text_msg.get('0.0', END))
text_msg.delete('0.0', END)
#创建几个frame作为容器
frame_left_top = Frame(width=380, height=270, bg='white')
frame_left_center = Frame(width=380, height=100, bg='white')
frame_left_bottom = Frame(width=380, height=20)
frame_right = Frame(width=170, height=400, bg='white')
##创建需要的几个元素
text_msglist = Text(frame_left_top)
text_msg = Text(frame_left_center);
button_sendmsg = Button(frame_left_bottom, text=unicode('发送','eucgb2312_cn'), command=sendmessage)
#创建一个绿色的tag
text_msglist.tag_config('green', foreground='#008B00')
#使用grid设置各个容器位置
frame_left_top.grid(row=0, column=0, padx=2, pady=5)
frame_left_center.grid(row=1, column=0, padx=2, pady=5)
frame_left_bottom.grid(row=2, column=0)
frame_right.grid(row=0, column=1, rowspan=3, padx=4, pady=5)
frame_left_top.grid_propagate(0)
frame_left_center.grid_propagate(0)
frame_left_bottom.grid_propagate(0)
#把元素填充进frame
text_msglist.grid()
text_msg.grid()
button_sendmsg.grid(sticky=E)
#frame_right 作为视频容器
#名字是output.avi'
# Define the codec and create VideoWriter object
#fourcc = cv2.cv.FOURCC(*'XVID')
#out = cv2.VideoWriter('output.avi', fourcc, 20, (640, 480))
#主事件循环
#在while循环中,利用摄像头对象的read()函数读取视频的某帧,
#并显示,然后等待1个单位时间,如果期间检测到了键盘输入q,则退出,即关闭窗口
while(1):
root.mainloop()
# get a frame
#这样 ret 存储布尔值,frame 存储图像
ret, frame1= cap.read()
# show a frame
cv2.imshow("video",frame1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#调用release()释放摄像头
cap.release()
#cv2.destroyAllWindows()
cv2.destroyWindow("video")