【问题标题】:Python 2.7 Tkinter Take Photo with CV2Python 2.7 Tkinter 用 CV2 拍照
【发布时间】: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


【解决方案1】:

如果您看到错误消息(“回溯”),则应完整显示(复制并粘贴)。

这一行:

screenTake = tk.Button(text='ScreenShot' ) , command = TakePictureC) #The button for take picture

有太多的')'。应该是:

screenTake = tk.Button(text='ScreenShot', command = TakePictureC) #The button for take picture

接下来,在函数内部,如果你给一个名字赋值,除非你另有说明,否则它会假设这个名字是本地的,所以当你尝试运行这个函数时:

def TakePictureC(): #There is the change of the variable

    takePicture  = takePicture + 1 #Add "1" to the variable

它会引发 NameError。

应该是:

def TakePictureC(): #There is the change of the variable
    global takePicture
    takePicture  = takePicture + 1 #Add "1" to the variable

最后,函数“show_frame”的主体应该缩进。

编辑:

你的回溯说:

NameError: name 'TakePictureC' is not defined

那是因为行:

screenTake = tk.Button(text='ScreenShot', command = TakePictureC)

说点击底部时调用TakePictureC,但是TakePictureC还没有定义。

【讨论】:

  • 我已经做了所有修改,控制台说:C:\Users***\Desktop\Programmations\Python-Programming\PyStoMo>python TestWeb1.p y Traceback(最近一次调用最后一次):文件“ TestWeb1.py", line 12, in screenTake = tk.Button(text='ScreenShot' , command = TakePictureC) #拍照的按钮 NameError: name 'TakePictureC' is not defined
猜你喜欢
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2012-01-31
  • 2016-11-06
  • 2017-12-08
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多