# Help needed with Python coding for taking photos with TouchPhat buttons

**URL:** https://forums.pimoroni.com/t/help-needed-with-python-coding-for-taking-photos-with-touchphat-buttons/5730
**Category:** Projects
**Created:** [August 21, 2017, 2:06pm UTC](https://forums.pimoroni.com/t/help-needed-with-python-coding-for-taking-photos-with-touchphat-buttons/5730 "2017-08-21T14:06:37Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Riverman](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/riverman/32/3012_2.png) [@Riverman](https://forums.pimoroni.com/u/Riverman)
#### Post date: [August 21, 2017, 2:06pm UTC](https://forums.pimoroni.com/t/help-needed-with-python-coding-for-taking-photos-with-touchphat-buttons/5730/1 "2017-08-21T14:06:37Z")

</div>

I have a TouchPhat and Noir camera attached to a PiZero w. I am attempting to take a still photo with the press of one button and a short video with another. The Python script I have attempted is below. I am getting a Syntax error on the ‘def my\_function()’ line and I cannot fathom why.  
Here is the full script. Excuse my ignorance as this is my first attempt at Python

```auto
import touchphat
import time
import picamera

camera = picamera.PiCamera()

while True:
    @touchphat.on_touch("A")
    def my_function()
        touchphat.led_on("A")
        camera.capture('image.jpg')
        touchphat.led_off("A")

    @touchphat.on_touch("B")
    def my_functiona()
        touchphat.led_on("B")
        camera.start_recording('video.h264')
        sleep(10)
        camera.stop_recording()
        touchphat.led_off("B")
except KeyboardInterrupt():
    sys.exit()

```

Thanks in advance for any help on this

---

<div class="post-metadata">

### Author: ![gadgetoid](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/gadgetoid/32/13_2.png) [@gadgetoid](https://forums.pimoroni.com/u/gadgetoid)
#### Post date: [August 21, 2017, 2:30pm UTC](https://forums.pimoroni.com/t/help-needed-with-python-coding-for-taking-photos-with-touchphat-buttons/5730/2 "2017-08-21T14:30:01Z")

</div>

While it’s not the cause of the problem, you should bind your Touch functions outside of your `while True:` loop, like so:

```auto
@touchphat.on_touch("A")
...

while True:
   ...

```

The syntax error you’re seeing is due to missing colons from the end of the function declarations.

It should be `def my_function():` rather than `def my_function()`

Ideally you should not actually do the recording _in_ the touch function, but rather set some sort of global state you can later rely on. Something like:

```auto
record = False
@touchphat.on_touch("B")
def start_record():
    global record
    record = True

while True:
    if record:
        touchphat.led_on("B")
        camera.start_recording("video.h264")
        sleep(10)
        camera.stop_recording()
        touchphat.led_off("B")
        record = False

```

You could then extend this to track that you’re currently recording, and have the single touch button toggle the recording on/off.

---

<div class="post-metadata">

### Author: ![Riverman](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/riverman/32/3012_2.png) [@Riverman](https://forums.pimoroni.com/u/Riverman)
#### Post date: [August 21, 2017, 9:22pm UTC](https://forums.pimoroni.com/t/help-needed-with-python-coding-for-taking-photos-with-touchphat-buttons/5730/3 "2017-08-21T21:22:52Z")

</div>

Thank you so much. I will study your code and apply it.

---

<div class="post-metadata">

### Author: ![The\_Handy\_Andy](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@The\_Handy\_Andy](https://forums.pimoroni.com/u/The_Handy_Andy)
#### Post date: [August 22, 2017, 3:33pm UTC](https://forums.pimoroni.com/t/help-needed-with-python-coding-for-taking-photos-with-touchphat-buttons/5730/4 "2017-08-22T15:33:11Z")

</div>

I will be ‘looking over your shoulder’! As I’ve just ordered (and recieved) a button shim to allow me to opperate the PiCam. What would interest me is to be able to use one of the buttons to power down the Pi.

---

<div class="post-metadata">

### Author: ![Riverman](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/riverman/32/3012_2.png) [@Riverman](https://forums.pimoroni.com/u/Riverman)
#### Post date: [August 22, 2017, 6:47pm UTC](https://forums.pimoroni.com/t/help-needed-with-python-coding-for-taking-photos-with-touchphat-buttons/5730/5 "2017-08-22T18:47:55Z")

</div>

Hi Andy. In that case I shall share my coding progress in this thread. I am new to Python and Pis so progress might be slow. I programmed in Basic back in the Spectrum days and have written some Java a few years back so we shall see how it goes…
