kivy app shows black screen on android phone - python

The kivy app that I have coded up won't work on my samsung galaxy s10e but will work on my computer. Maybe I need to add somthing to my .spec file but I dont know
Here is the code for the app:
import kivy
kivy.require('2.1.0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
import random
class MyRoot(BoxLayout):
def __init__(self):
super(MyRoot, self).__init__()
def generate_number(self):
self.random_label.text = str(random.randint(0, 1000))
class randomnumber(App):
def build(self):
return MyRoot()
randomint = randomnumber()
randomint.run()

Related

Kivy(framework): blank window after running code

After running this code python idle gave me a blank window without any kivy widgets and python idle does not show any error.
what is the problem in this code?
screen shot of blank window
code:
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
class me(App):
def __init__(self,b,g,l,t):
super(me, self).__init__()
self.b=Button(text='start')
self.g=GridLayout(cols=4)
self.l=Label(text='label')
self.t=TextInput()
self.g.add_widget(self.b)
self.g.add_widget(self.t)
self.g.add_widget(self.l)
m=me('b','g','l','t')
m.run()
Building the App should be done in a build() method of the App, and that method should return the root widget for the App. Like this:
class me(App):
def __init__(self,b,g,l,t):
super(me, self).__init__()
self.b=Button(text='start')
self.g=GridLayout(cols=4)
self.l=Label(text='label')
self.t=TextInput()
self.g.add_widget(self.b)
self.g.add_widget(self.t)
self.g.add_widget(self.l)
def build(self):
return self.g

How to use android recipe?

How to use android recipe. There is a file on the off github (https://github.com/kivy/python-for-android/blob/master/pythonforandroid/recipes/android/__init__.py), but I don't know how to attach it to my project. More precisely, how to fit android.runnable into buildozer requirements
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.utils import platform
from kivy.uix.widget import Widget
from kivy.clock import Clock
from jnius import autoclass
from android.runnable import run_on_ui_thread
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.kivy.android.PythonActivity').mActivity
class Wv(Widget):
def __init__(self, **kwargs):
super(Wv, self).__init__(**kwargs)
Clock.schedule_once(self.create_webview, 0)
#run_on_ui_thread
def create_webview(self, *args):
webview = WebView(activity)
webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient();
webview.setWebViewClient(wvc);
activity.setContentView(webview)
webview.loadUrl('http://www.google.com')
class ServiceApp(App):
def build(self):
return Wv()
if name == 'main': ServiceApp().run()

How can I play a video with kivy v2.0.0?

I want to play a video with Kivy 2.0.0 and Python 3.6, but running this code will give me a load error because Kivy's video player won't recognize the file. How can I get Kivy's video player to recognize the file?
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.videoplayer import VideoPlayer
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.video import Video
from kivy.lang.builder import *
from kivy.app import App
import kivy
import os
kivy.require('2.0.0')
# Main page code
class MainPage(Screen):
""" This is the code for the Main Screen """
# Initialization
def __init__(self, **var_args):
# Constructor for the Main Page class
super(MainPage, self).__init__(**var_args)
# Layout
self.layout = FloatLayout()
# Video player
self.filename = '/'
self.player = VideoPlayer(source=self.filename,state='play',options={'allow_stretch': True})
self.layout.add_widget(self.player)
# Add the layout to the screen
self.add_widget(self.layout)
# Main app class
class Video_Viewer(App):
""" Main build """
# Main build
def build(self, **kwargs):
# Create the screen manager
self.manager = ScreenManager()
# Add screens to screen manager
self.manager.add_widget(MainPage(name='Main'))
# Set the screen to the main sceren
self.manager.current = 'Main'
# Return the current screen of the screen manager
return self.manager
# Run the file
if __name__ == '__main__':
Video_Viewer().run()
This is the error that happens as a result of the code:
[ERROR ] [Image ] Error loading <C:/Users/My Laptop/Downloads/video.mp4>
Try to install ffpyplayer
python -m pip install ffpyplayer

How do I open Google Earth within a Python Kivy app?

I am trying to open Google Earth within my Python Kivy app. I have tried:
webbrowser.open ("https://www.google.com/earth/")
However, this opens Google Earth in a separate tab. I want it to open the link within the app rather than it redirecting elsewhere. Researching this I came across this code:
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.utils import platform
from kivy.uix.widget import Widget
from kivy.clock import Clock
from jnius import autoclass
from android.runnable import run_on_ui_thread
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity =
autoclass('org.kivy.android.PythonActivity').mActivity
class Wv(Widget):
def __init__(self, **kwargs):
super(Wv, self).__init__(**kwargs)
Clock.schedule_once(self.create_webview, 0)
#run_on_ui_thread
def create_webview(self, *args):
webview = WebView(activity)
webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient();
webview.setWebViewClient(wvc);
activity.setContentView(webview)
webview.loadUrl('http://www.google.com')
class ServiceApp(App):
def build(self):
return Wv()
if __name__ == '__main__':
ServiceApp().run()
https://github.com/kivy/kivy/wiki/Android-native-embedded-browser
However, this will not let me make this import statement:
from android.runnable import run_on_ui_thread
from android.runnable import run_on_ui_thread
ModuleNotFoundError: No module named 'android'

Kivy Text Editor Input Not Showing Up

I have unfortuanlly have encourtered an error in kivy and Python 3. I have not found a soultion via Google. I wanted to get text input at the very least but it does not show up. Just the text itself. Thank you for your time!
import kivy
kivy.require('1.10.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
class ColdKivyApp(App):
def build(self):
f = FloatLayout()
label = Label(text="Cold") #I acutally orginally called it Zone unitil I changed it into Cold cause it's really cold now
f.add_widget(label)
txt = TextInput(text='', focus=True, multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
return f
if __name__ == '__main__':
ColdKivyApp().run()
It seems that there is a bug in TextInput when setting the focus in the constructor, a workaround is to set the focus an instant after the window is shown through Clock:
import kivy
kivy.require('1.10.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.clock import Clock
class ColdKivyApp(App):
def build(self):
f = FloatLayout()
label = Label(text="Cold")
f.add_widget(label)
txt = TextInput(multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
Clock.schedule_once(lambda *args: setattr(txt, "focus", True))
return f
if __name__ == '__main__':
ColdKivyApp().run()

Categories

Resources