The code uses a toggle style button to return a value of on/off. The toggle seems to function great when returning print() but has trouble when fed camera code. preview.start_recording('video_%d', format='h264') activates the recording. When the toggle switches and preview.stop_recording() is activated the following error arises, it is unable to stop filming:

Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
[GCC 4.9.1] on linux
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> Rec
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.4/idlelib/run.py", line 121, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "/usr/lib/python3.4/queue.py", line 175, in get
raise Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
return self.func(*args)
File "/home/pi/Desktop/PiCam/PiCam(Beta v0.01)", line 10, in toggle
preview.resolution = (1920,1080)
File "/usr/lib/python3/dist-packages/picamera/camera.py", line 2271, in        
File "/usr/lib/python3/dist-packages/picamera/camera.py", line 772, in  
raise PiCameraRuntimeError("Recording is currently running")
picamera.exc.PiCameraRuntimeError: Recording is currently running

The following is the program itself written in Python 3 version 3.4.2

import picamera
import itertools
from tkinter import *

def toggle():
    state = next(icycle)
    button1['text'] = str(state)
    preview.exposure_mode = 'auto'
    preview.resolution = (1920,1080)
    preview.framerate = (30)

    if state == 'Rec': ##this means the camera is on
       button1['relief'] = "raised"
       button1['bg'] =  "red"

    elif state == 'Stop':  #this means the camera is off
       print ("Rec")
       button1['relief'] =  "sunken"
       button1['bg'] =  "white"
       preview.start_recording('video_%d', format='h264')

def snap():
    print ("photo!")

def kill():

root = Tk()

root.title ("PiCam.Beta(V 0.01")

icycle = itertools.cycle(['Stop', 'Rec'])
button1 = Button(master=None,bg='red', relief = "raised",    text = "Rec",      
width=5,  command=toggle) #film
button1.place(x=410, y=290)

button2 = Button(master=None, activebackground='yellow',text="Snap Shot",     
command=snap) #snapshot
button2.place(x=320, y=290)

button3 = Button(master=None,activebackground='white',  text = "Exit",  
command=kill) #exit button
button3.place(x=0, y=290)

preview= picamera.PiCamera()
preview.preview_window=(579, 412, 480, 313)

Thank you in advance!!

  • are you running your code with sudo python yourcode.py? – rob May 8 '16 at 15:16
  • currently I am running the code in the Python 3 IDEL and not the command line. If the code does not perform properly in the IDEL test then I do not see a reason it should run properly on the command line. The file it creates never stops recording until the program is shut down. I can confirm this because the file continues to get larger until I have to delete it. – Rent-A -Gent May 8 '16 at 16:37

TL;DR: The problem is that you're trying to set the resolution and framerate properties while you're recording.

The toggle method you've written starts by setting exposure_mode which is fine (as the documentation states: "The property can be set while recordings or previews are in progress"). However, it then goes on to set resolution which the documentation notes "...no recording must be active when the property is set". I'd guess you want to set the resolution and framerate at the top of your program and not every time the toggle button is hit.

Incidentally, the same rule applies to framerate and sensor_mode (and the clock_mode property that's forthcoming in 1.11) but I don't think there's much else that can't be adjusted while a recording is running.

Generally, a good hint for the way to debug these things is to look back through the stack trace and find the first line that exists in code you've written (this assumes that the mistake is in code you've written as opposed to an upstream library, but regardless of the coder's skill that's a decent assumption so it's a reasonable starting point). In this case if we work back from the end of the stack trace we get to:

File "/home/pi/Desktop/PiCam/PiCam(Beta v0.01)", line 10, in toggle preview.resolution = (1920,1080)

At that point you know the following:

  • A line setting the resolution is what triggered the error
  • The error is "Recording is currently running"

At that point you can conclude that it's probable that the resolution can't be set while a recording is running (even if it's not changing the resolution, perhaps the mere act of setting it is forbidden). That's the point where I'd check the docs for the resolution property (linked above) to see if my hunch was correct (that's also the point where you discover whether the documentation for the library you're using is any good ;).

| improve this answer | |
  • Thank you very much for the expertise! This sounds like a great solution and in the future I will pay more attention to the top section of the error code. I will try this and let you know the results asap! Thanks again! – Rent-A -Gent May 8 '16 at 18:23
  • Glad it worked :) Incidentally, thanks for posting a well written question with a full stack-trace - helps immensely in figuring things out! – Dave Jones May 9 '16 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.