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

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'

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

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

close webview in kivy

iam trying to close or destroy the webview and show other layout
here is my code for webview or if that won't possible i want to get the link of the current page and if current page link is equal to some link i want to exit the webview pls help me out this
from android.runnable import run_on_ui_thread as run_thread
from jnius import autoclass
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.app import runTouchApp
from kivy.clock import Clock, mainthread
#mainthread
def quit_screen():
app = App.get_running_app()
e=GridLayout()
d1=button(text="hi end")
e.add_widget(e)
app.root.switch_screen(e)
#run_thread
def key_back_handler():
webview.loadUrl("about:blank")
webview.clearHistory()
webview.clearCache(True)
webview.clearFormData()
webview.freeMemory()
Clock.schedule_once(quit_screen, 0)
#run_thread
def WebView(link,*args):
WebV = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.kivy.android.PythonActivity').mActivity
webview = WebV(activity)
settings = webview.getSettings()
settings.setJavaScriptEnabled(True)
settings.setUseWideViewPort(True)
settings.setLoadWithOverviewMode(True)
settings.setSupportZoom(True)
settings.setBuiltInZoomControls(True)
wvc = WebViewClient()
webview.setWebViewClient(wvc)
activity.setContentView(webview)
webview.loadUrl(link)
m=GridLayout(cols=1,rows=1)
d=GridLayout(cols=1,rows=1)
m.add_widget(d)
def Push(butoon):
WebView("https://www.google.com")
b=Button(text="hi")
b.bind(on_press=Push)
d.add_widget(b)
runTouchApp(m).run()
like this but its not closing on back pressed on android
Thanks for help in advance
The trick is to put the Webview in a Kivy ModalView and catch the back button/gesture from Java to exit the ModalView , for example:
https://github.com/RobertFlatt/Android-for-Python/tree/main/webview

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

Categories

Resources