Dahaarl
Dahaarl

Reputation: 53

Python Kivy - App that open url in a native web browser

I try to make a simple app that open a web page inside Kivy after clicking a button placed on a "Screen One".

I used this topic (Python - Showing a web browser/iframe right into the app) as reference but I didn't understand how to use the code provided by Michael...

So I tried this... and when I launch the apk (build with Buildozer) it didn't work :')

import kivy
kivy.require('1.9.2')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

# MICHAEL'S CODE
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.renpy.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/')
# END OF MICHAEL'S CODE

Builder.load_string('''

<ScreenOne>:
    BoxLayout:
        Label:
            text: "SCREEN 1"
        Button:
            text: "GO GO GO TO GOOGLE !"
            on_press: root.open_browser()

<ScreenTwo>:
    BoxLayout:
        Label:
            text: "SCREEN 2"
        Button:
            text: "GO GO GO TO SCREEN 1"
            on_press:
                root.manager.transition.direction = "right"
                root.manager.transition.duration = 1
                root.manager.current = "screen_one"

''')

class ScreenOne(Screen):
    def open_browser(self):
        return Wv()

class ScreenTwo(Screen):
    pass

screen_manager = ScreenManager()

screen_manager.add_widget(ScreenOne(name="screen_one"))
screen_manager.add_widget(ScreenTwo(name="screen_two"))


class BrowserApp(App):
    def build(self):
        return screen_manager

app = BrowserApp()
app.run()

The app don't crash but just close when I start it.

What I'm doing wrong ? I'm sure that I don't use it the right way...

Log from adb logcat:

06-13 12:54:47.559 7429 7510 I python : ImportError: No module named android

06-13 12:54:47.579 7429 7510 I python : Python for android ended.

Upvotes: 4

Views: 11927

Answers (1)

Peter Badida
Peter Badida

Reputation: 12189

From the log you posted in the comments I extracted the two important lines:

06-13 12:54:47.559 7429 7510 I python : ImportError: No module named android
06-13 12:54:47.579 7429 7510 I python : Python for android ended.

This basically means that the copied code:

from android.runnable import run_on_ui_thread

won't work because it doesn't detect the android module. The module has a separate recipe, therefore you will need to add it to the requirements, so that it compiles the Cython code and adds it to your app, otherwise the import will always fail.

Basically you always want to search for 3-4 keywords when looking into such a messy logcat → "python", "Traceback", "Python for android", "kivy". There's a filter in buildozer for that if you use it:

android.logcat_filters = *:S python:D

Upvotes: 6

Related Questions