I want to make a Python 2.7 Tkinter take webcam photo but I can't arrived at the end of a problem: my program don't want to read a variable and execute it. Can you help me?
This is my code:
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
Thank's for help and have a good week!(Sorry for my very bad english, I'm a french student :/ )
My error message:
C:\\Users***\\Desktop\\Programmations\\Python-Programming\\PyStoMo>
python TestWeb1.py
Traceback (most recent call last): File "TestWeb1.py", line 12, in screenTake = tk.Button(text='ScreenShot' , command = TakePictureC)#The butt on for take picture
NameError: name 'TakePictureC' is not defined
If you're seeing an error message ("traceback"), you should show it in full (copy-and-paste it).
This line:
screenTake = tk.Button(text='ScreenShot' ) , command = TakePictureC) #The button for take picture
has too many ')'. It should be:
screenTake = tk.Button(text='ScreenShot', command = TakePictureC) #The button for take picture
Next, inside a function, if you assign to a name, it will assume that the name is local unless you say otherwise, so when you try to run this function:
def TakePictureC(): #There is the change of the variable
takePicture = takePicture + 1 #Add "1" to the variable
it'll raise a NameError.
That should be:
def TakePictureC(): #There is the change of the variable
global takePicture
takePicture = takePicture + 1 #Add "1" to the variable
Finally, the body of the function 'show_frame' should be indented.
EDIT:
Your traceback says:
NameError: name 'TakePictureC' is not defined
That's because the line:
screenTake = tk.Button(text='ScreenShot', command = TakePictureC)
says to call TakePictureC when the bottom is clicked, but TakePictureC hasn't been defined yet.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.