Home » Android » android – How to run camera.py code in homescreen.kv file in kivy-Exceptionshub

android – How to run camera.py code in homescreen.kv file in kivy-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

this is my camera.py file

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import time
Builder.load_string('''
<CameraClick>:
   orientation: 'vertical'
   Camera:
       id: camera
       resolution: (640, 480)
       play: False
   ToggleButton:
       text: 'Play'
       on_press: camera.play = not camera.play
       size_hint_y: None
       height: '48dp'
   Button:
       text: 'Capture'
       size_hint_y: None
       height: '48dp'
       on_press: root.capture()
''')


class CameraClick(BoxLayout):
   def capture(self):
       '''
       Function to capture the images and give them the names
       according to their captured time and date.
       '''
       camera = self.ids['camera']
       timestr = time.strftime("%Y%m%d_%H%M%S")
       camera.export_to_png("IMG_{}.png".format(timestr))
       print("Captured")


class TestCamera(App):

   def build(self):
       return CameraClick()


TestCamera().run()

how can I add this camera-python file as an event to a button on my home screen
Can anyone please help me?

this is my homescreen.kv file

#:import utils kivy.utils

<HomeScreen>:
    FloatLayout:
        canvas:
            Color:
                rgb: utils.get_color_from_hex("#f0eed1")
            Rectangle:
                source: "icons/blur.png"
                size: self.size
                pos: self.pos
        GridLayout:
            rows: 1
            pos_hint: {"top": 1, "left": 1}
            size_hint: 1, .1
            Image:
               source: "icons/search.png"
            Image:
                source: "icons/google.png"
        GridLayout:
            rows: 1
            pos_hint: {"top": .9,"left": 1}
            size_hint: 1, .2
            Image:
               source: "icons/003-sunset.png"
            Image:
                source: "icons/icon.png"
            Image:
                source: "icons/003-sunset.png"            
        Label:
            pos_hint: {"top": .7,"left": 1}
            size_hint: 1, .1
            text: "MediInfo"
            font_size: 30
            id: medi_label
            color: 1,1,1,1
        GridLayout:
            cols: 1
            pos_hint: {"top": .6,"left": 1}
            size_hint: 1, .4
        GridLayout:
            rows: 1
            pos_hint: {"top": .3,"left": 1}
            size_hint: 1, .1
            ImageButton:
                source: "icons/search.png"
                on_release:
                    app.change_screen("search_screen")
            ImageButton:
                source: "icons/camera.png"
                on_release:
                    # I want to run camera.py file on click of this button


        Label:
            pos_hint: {"top": .2,"left": 0}
            size_hint: 1, .07
            text: "upload file"
            id: medi_label
            color: 1,1,1,1

I want to launch camera after clicking the camera button in my home screen of this app but I’m not understanding how to do it is this possible?
Thank you.

How to&Answers: