How to use android recipe? - python

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()

Related

kivy app shows black screen on android phone

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()

Python app framework create webview inside of app

I need to embed a webview inside my kivy app or with some other python app framework that supports this for both android and IOS. I am looking to also save cookies for log in so users do not have to log in more than once. Is this possible or should I look for another way to accomplish this? Thanks for any responses in advance!
Unfortunately there is no "universal" method to do this. It should however still be possible. There unfortunately isn't a kivy "native" method either.
Android:
For android you can use webview-android:
from kivy.uix.widget import Widget
from kivymd.app import MDApp
from webview import WebView
from kivy.lang.builder import Builder
from kivymd.uix.button import MDFlatButton
from kivymd.uix.screen import MDScreen
Builder.load_string("""
<MyWebView>
MDFlatButton:
text: "Push"
pos_hint: {"center_x": .5, "center_y": .4}
on_press: root.Push()
""")
class MyWebView(MDScreen):
def Push(self):
WebView("https://www.google.com")
class MyWebApp(MDApp):
def build(self):
return MyWebView()
if __name__ == '__main__':
MyWebApp().run()
Furthermore, you can use jnius to access the java classes, that would do this normally:
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()
iOS:
Unfortunately I don't own an iOS device, so I can not test any of this.
The kivy-ios module seems to contain methods for accomplishing this:
import ios
url = "http://www.google.com"
ios.IOSWebView().open(url, width, height)
Another solution would be to use pyobjus to access the Objective-C classes, that would normally implement the webview on iOS. I don't want to paste untested code, so I suggest you check out Michael Galaxy and Julez' answers on the bottom of this google group.

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'

Displaying an MDList in my kivymd file doesn't work

I'm working on a project with python and kivymd and i want to display an MDList in my file kv (list.kv)
but it doesn't work , i don't know where is the problem !! any suggestions
this is main.py
from kivy.lang import Builder
from mysql.connector import Error
from baseclass.start import Start
from kivy.lang import Builder
from threading import Thread
from kivy.uix.screenmanager import Screen
from kivy.utils import get_color_from_hex
from spin_load import ProgressSpinner
from kivymd.color_definitions import colors
from kivy.uix.recycleview import RecycleView
from kivy.core.window import Window
from kivy.uix.recycleview import RecycleView
import mysql.connector
from kivymd.uix.menu import MDDropdownMenu
from kivy.properties import OptionProperty
from kivy.properties import ObjectProperty
from kivymd.uix.bottomsheet import MDListBottomSheet
from kivymd.uix.expansionpanel import MDExpansionPanel
from kivymd.uix.boxlayout import BoxLayout
from kivymd.uix.list import OneLineListItem
Window.size = (360, 600)
class Codebarre(Screen):
def on_start(self):
for i in range(20):
self.root.ids.container.add_widget(
OneLineListItem(text=f"Single-line item {i}")
)
class MyApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "DeepPurple"
return Builder.load_file("main.kv")
MyApp().run()
and this is the file where i want to display the MDList
list.kv
#:import utils kivy.utils
<Codebarre>:
name: 'codebarre'
ScrollView:
MDList:
id: container ```
It turns out that the on_start() method was never called. A working version of main.py is below. I didn't change list.kv.
from kivy.lang import Builder
from threading import Thread
import kivy.clock
from kivy.uix.screenmanager import Screen
from kivy.utils import get_color_from_hex
from kivymd.color_definitions import colors
from kivy.uix.recycleview import RecycleView
from kivy.core.window import Window
from kivy.uix.recycleview import RecycleView
from kivymd.uix.menu import MDDropdownMenu
from kivy.properties import OptionProperty
from kivy.properties import ObjectProperty
from kivymd.uix.bottomsheet import MDListBottomSheet
from kivymd.uix.expansionpanel import MDExpansionPanel
from kivymd.uix.boxlayout import BoxLayout
from kivymd.uix.list import OneLineListItem
from kivymd.app import MDApp
from kivy.uix.stacklayout import StackLayout
Window.size = (360, 600)
class Codebarre(Screen):
def __init__(self, **kvargs):
self.app = MDApp.get_running_app()
super().__init__(**kvargs)
kivy.clock.Clock.schedule_once(self.build)
def build(self, *args):
for i in range(20):
self.ids.container.add_widget(
OneLineListItem(text=f"Single-line item {i}")
)
class MyApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "DeepPurple"
Builder.load_file("list.kv")
return Codebarre()
MyApp().run()

Categories

Resources