kivy widgets doesn't appear just show black screen - python

I am a new learner of the Kivy module.I am practicing making a selfie app by following a video, by clicking on the button, the camera takes a frame and export it as a PNG image. in code I am adding widget (camera, button), and what show is a black screen. so what should I do?
import cv2
from kivy.app import App
from kivy.uix.camera import Camera
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class selfie_app(App):
def make(self):
self.obj_camera = Camera()
self.obj_camera.resolution = (810, 810)
obj_button = Button(text="Click !!")
obj_button.size_hint = (.5,.2)
obj_button.pos_hint = {'x':.15, 'y':.15}
obj_button.bind(on_press = self.selfie_take())
obj_layout = BoxLayout()
obj_layout.add_widget(obj_button)
obj_layout.add_widget(self.obj_camera)
return obj_layout
def selfie_take(self, *args):
print("selfie taken successfully !")
self.obj_camera.export_to_png('./demo_file.png')
if __name__ == '__main__':
selfie_app().run()

Related

Video is not playing and only Audio is playing in kivy

i am trying to create a video splash screen but when i play the video only only is played and screen is blank. The video format is mp4. Also can you help me change the to a different screen after the video ends
How do i go about it?
from kivy import *
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.uix.videoplayer import VideoPlayer
import time
from ffpyplayer import *
# audio
import os
from playsound import playsound
import threading
class SplashScreen(Screen):
# def on_enter(self, *args):
# Clock.schedule_once(self.switch_to_home, 5)
# splash_video()
def switch_to_home(self, dt):
self.manager.current = "home"
def splash_video():
video = VideoPlayer(source = '/home/devesh/Desktop/readzy-app/images/logo(watermark).mp4')
video.state = 'play'
video.options = {'eos': 'switch_to_home()'}
return video
splash_video()
home_page = Builder.load_file("Readzy.kv")
# ! main app class
class Readzy(App):
def build(self):
sm = ScreenManager()
sm.add_widget(SplashScreen(name='splash'))
sm.add_widget(HomeScreen(name='home'))
sm.add_widget(ReadingScreen(name='reading'))
sm.add_widget(WPSScreen(name="wps"))
return sm
# run the app
if __name__ == "__main__":
Readzy().run()```
I hv tried mpeg format but the result is same

os.system() don't let updating image source in kivy python

I am trying to update the image source on a changeImageSource function it changes the source instantly but when I use time.sleep() method in that function, function executes but doesn't update the source of the image. updates after time.sleep() call completed.
from kivy.app import App
from kivy.uix.image import AsyncImage
from kivy.uix.button import Button
from kivy.uix.widget import Widget
import time
# creating the App class
class MyApp(App):
def build(self):
parent = Widget()
#creating and adding image to widget
self.img = AsyncImage(
source='http://kivy.org/logos/kivy-logo-black-64.png')
self.img.pos = (400,400)
#creating btn and adding press handler
self.change_img_btn = Button(text="Change Image ")
self.change_img_btn.bind(on_press = self.changeImageSource)
#adding widget to Widget instance
parent.add_widget(self.img)
parent.add_widget(self.change_img_btn)
return parent;
def changeImageSource(self,*args):
self.img.source = "https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a"
time.sleep(4)
# run the App
MyApp().run()
One: The "return parent;" should be "return parent"
Two: Why do you need time.sleep()?
You can also try flipping line 29 and 30.

How to add background image in Kivy without kv language

I'm creating an Kivy App for Desktop. I've created most of the app but I want to add a background image to the app. I've not use the KV language but created all the widgets using Python code only.
Can anybody please help me adding a background image in the kivy app using Python.
You can use with canvas: to draw a background image. Here is a simple example:
from kivy.app import App
from kivy.clock import Clock
from kivy.graphics.vertex_instructions import Rectangle
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
class TestApp(App):
def build(self):
theRoot = FloatLayout()
# draw the background
with theRoot.canvas:
self.rect = Rectangle(source='background.png')
# use binding to insure that the background stay matched to theRoot
theRoot.bind(on_size=self.update)
theRoot.add_widget(Label(text="Hi", size_hint=(None, None), size=(100, 50), pos=(100,100)))
# need to call update() to get background sized correctly at start
Clock.schedule_once(self.update, -1)
return theRoot
def update(self, *args):
# set the size and position of the background image
self.rect.size = self.root.size
self.rect.pos = self.root.pos
TestApp().run()

Kivy image not showing

Im working on a basic app using python and kivy and im trying to import an image which I'm basically going to use as a splash screen of sorts but for some reason I couldn't get it to work so I created a .py file and wrote some very simple code to try to get it to work:
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
label = Label(source="image.jpg")
return label
if __name__ == "__main__":
ImageTestApp().run()
The image is in the same directory of the .py file and the name of the image is exactly the same, I even made the window size the same as the image size and tried .PNG but nothing worked 😖
Label does not have an attribute called source. You should change Label to Image, and it will works. Please refer to example below for details.
Example
main.py
from kivy.app import App
from kivy.uix.image import Image
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
return Image(source="image.jpg")
if __name__ == "__main__":
ImageTestApp().run()
Output

How can I reload a image in kivy python

this is my script ...
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
class MyApp(App):
def build(self):
return Image(source='go.jpg')
MyApp().run()
I won't to reload it becose the image is changing and I won't to see the new won all time
you can use reload() the read the image from the disk again
this will reload even if the original data of the image is updated or changed
self.ids.image1.source = './Images/file.png'
self.ids.image1.reload()
Make your own widget class, and make the image an attribute, so you can reference it. Then use clock to schedule an interval method, to constantly reload the image.
In the example below, the update_pic method is executed once every second.
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.uix.widget import Widget
class MyImageWidget(Widget):
def __init__(self,**kwargs):
super(MyImageWidget,self).__init__(**kwargs)
self.image = Image(source='go.jpg')
self.add_widget(self.image)
Clock.schedule_interval(self.update_pic,1)
def update_pic(self,dt):
self.image.reload()
class MyApp(App):
def build(self):
return MyImageWidget()
MyApp().run()
You can use the Image.reload method
def build(self):
img = Image(source='go.jpg')
Clock.schedule_interval(lambda dt: img.reload(), 0.2) #5 per second
return img

Categories