【发布时间】:2015-02-25 05:14:51
【问题描述】:
我想制作一个 Python 2.7 Tkinter 拍摄网络摄像头照片,但我无法解决问题:我的程序不想读取变量并执行它。你能帮帮我吗?
这是我的代码:
import Tkinter as tk
import cv2
from PIL import Image, ImageTk
width, height = 800, 600 #Initialisation of webcam size
cap = cv2.VideoCapture(0) #Type of Capture
takePicture = 0 #My variable
lmain = tk.Label() #The Tk widget of webcam
lmain.pack() #Pack option to see it
screenTake = tk.Button(text='ScreenShot' ) , command = TakePictureC) #The button for take picture
screenTake.pack() #Pack option to see it
def TakePictureC(): #There is the change of the variable
takePicture = takePicture + 1 #Add "1" to the variable
def show_frame(): #CV2 webcam parameters
_, frame = cap.read()
frame = cv2.flip(frame, 1)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
lmain.after(10, show_frame)
if takePicture == 1: #My option for take the image
img.save("test.png") #Save the instant image
takePicture = takePicture - 1 #Remove "1" to the variable
show_frame() #CV2 show the webcam module
root = tk.Tk() #basic parameters
root.bind('<Escape>', lambda e: root.quit())
root.title( "title" )
root.mainloop() #end of the program
感谢您的帮助,祝您度过愉快的一周!(对不起,我的英语很差,我是法国学生:/)
我的错误信息:
C:\Users***\Desktop\Programmations\Python-Programming\PyStoMo>
python TestWeb1.py
Traceback (最近一次调用最后一次): File "TestWeb1.py", line 12, in screenTake = tk.Button(text='ScreenShot' , command = TakePictureC)#拍照的按钮
NameError: 名称“TakePictureC”未定义
【问题讨论】:
-
那么...您收到错误消息还是什么?
标签: python python-2.7 tkinter webcam