Insert Python code in Kivy code

87 views
Skip to first unread message

Siegfried Maps

unread,
Sep 5, 2017, 1:09:09 AM9/5/17
to Kivy users support
hello !

i am trying to build a photobooth app with a raspberry pi connected to a HDMI screen:

i created a function who take a photo and another function who print it. Both function are controlled with physical push buttons connected to the raspberry via GPIO:

the photo function:
def photo_callback():
       
# Define filename with timestamp
        photoName
= time.strftime("%Y%m%d%H%M%S") + "_photobooth.jpg"
        GPIO
.output(DoneLedPin, GPIO.LOW)
        GPIO
.output(waitLedPin, GPIO.HIGH)
       
....
       
....
       
Image.open(photoPath + photoName).resize(photoNotResize, Image.ANTIALIAS).save(photoPath + "wallpaper.jpg")


the print function:
def print_callback():
       
# Rotate the thumbnail for printing
       
Image.open(photoPath + "thumbnail.jpg").transpose(0).save(photoPath + "thumbnail-rotated.jpg")
        printer
.begin(90) # Warmup time
        printer
.setTimes(40000, 2000) # Set print and feed times
        printer
.justify('C') # Center alignment
       
#printer.printImage(Image.open(photoPath + "line-top.png"), True)
        printer
.feed(2)
        printer
.setSize('S')
        printer
.println(photoTitle)
       
#printer.printImage(Image.open(photoPath + "line-bottom.png"), True)
        printer
.feed(2) # Add a blank line
        printer
.printImage(Image.open(photoPath + "thumbnail-rotated.jpg"), True) # Specify image to print
       
....
       
....


Everything is working great with a physical button (connected to the Raspberry PI with GPIO) with this loop:

while True:
    input_state
= GPIO.input(buttonPhotoPin)
   
if input_state == False:
       
print('Photo')
        photo_callback
()
        time
.sleep(2)
        print_callback
()
        time
.sleep(0.2)


I wanted to display the last taken picture on a HDMI display using Kivy. I found an example with a video but can't find a way to use it correctly myself: I don't want any interface buttons displayed on the screen, rather just the last picture (wallpaper.jpg) and that's it.

class MyApp(App):
       
# Display the latest thumbnail
        photo
= kivyImage(source="/home/pi/photobooth/wallpaper.jpg")
               
if __name__ == '__main__':
       
MyApp().run()



I have problem on displaying the picture on the display and making my "while" loop working together:

With this code, just the while loop ist working - the last picture is not working with kivy:

while True:
    input_state
= GPIO.input(buttonPhotoPin)
   
if input_state == False:
       
print('Photo')
        photo_callback
()
        time
.sleep(2)
        print_callback
()
        time
.sleep(0.2)

class MyApp(App):
       
# Display the latest thumbnail
        photo
= kivyImage(source="/home/pi/photobooth/wallpaper.jpg")
               
if __name__ == '__main__':
       
MyApp().run()


i tried to insert the while loop in the kivy code, but here, just the image is displayed and i can't get the physical button to work:

class MyApp(App):
       
# Display the latest thumbnail
        photo
= kivyImage(source="/home/pi/photobooth/wallpaper.jpg")
       
       
while True:
            input_state
= GPIO.input(buttonPhotoPin)
           
if input_state == False:
               
print('Photo')
               photo_callback
()
               time
.sleep(2)
               print_callback
()
               time
.sleep(0.2)

if __name__ == '__main__':
       
MyApp().run()


tl;dr: I only manage to make the last picture displayed OR take a photo and print it, but not both.

what am i doing wrong?
i made researches and think i have to define a special function (the push button function) and call it in a certain way in Kivy, but i can't find how.

thank you in advance for your help!

greetings

alrick





Jacek Blocki

unread,
Sep 5, 2017, 3:33:13 AM9/5/17
to Kivy users support
Have a look at App class documentation (https://kivy.org/docs/api-kivy.app.html). You need to write build() method in App returning widget tree. Kivy is based on event loop, while loop will block it so run it in a thread.
BR, Jacek

Rufus Smith

unread,
Sep 21, 2017, 2:12:56 PM9/21/17
to Kivy users support
Where do you wait for the picture button to be released?  Or do you want pictures to be taken repeatedly while
the button is held down?

Also, as far as I know, Kivy doesn't know when an image file on disk has been changed, so you will have to tell
it when it happens, in order for it to refresh the picture.

Read up on the threading module in python, and you can monitor your button in a separate thread.
Alternatively, you can set a timer and periodically check the state of the button within kivy.  But no
while True: loops in the main thread!

First of all, build your root widget explicitly and display your wallpaper there, as Jacek suggests.



Reply all
Reply to author
Forward
0 new messages