Python - Showing a web browser/iframe right into the app

时光总嘲笑我的痴心妄想 提交于 2019-12-21 06:15:21

问题


I have a script which if helpful to people answering questions, is using kivy. I want to have it show a iframe kind of thing right into it when run, instead of opening the browser. For example something like this:

def browser():
    url = "google.com"
    iframe(url)
browser()

Obviously this wouldnt work as python is not html. Keep in mind, I am not trying to run this script on the web, but on the kivy launcher. As intended, it should not open the webbrowser but instead show the page in a box built right into the script.


回答1:


You are trying to do this on an android device? There's currently not a build in way to do this, but I'm pretty sure it's possible to load a native android webview via pyjnius. I'm not sure the current state, but for example here is an example of how to do it. I've pasted some of the code below, but I recommend asking on the kivy mailing list or irc if you have any questions, as this kind of thing is under discussion and development.

from android.runnable import run_on_ui_thread

WebView = autoclass('android.webkit.WebView')
LayoutParams = autoclass('android.view.ViewGroup$LayoutParams')
activity = autoclass('org.renpy.android.PythonActivity').mActivity

class Wv(Widget):
    def __init__(self, **kwargs):
        super(Wv, self).__init__(**kwargs)
        self.create_webview()

    @run_on_ui_thread
    def create_webview(self):
        webview = WebView(activity)
        activity.addContentView(webview, LayoutParams(-1, -1))
        webview.getSettings().setJavaScriptEnabled(True)
        #having some trouble with this one: webview.setBackgroundColor(Color.TRANSPARENT)

        html = "<html><body style='margin:0;padding:0;'>\
            <script type='text/javascript'\
            src='http://ad.leadboltads.net/show_app_ad.js?section_id=ID_HERE'>\
            </script></body></html>"    

        activity.setContentView(webview)
        webview.loadData(html, "text/html", "utf-8")
        layout = LinearLayout(activity)
        layout.addView(activity.mView)
        activity.setContentView(layout)



回答2:


Here's an actual running example which works right inside the "Kivy Launcher" app:

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.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')

class ServiceApp(App):                                                                          
    def build(self):                                                                            
        return Wv()                                                                             

if __name__ == '__main__':                                                                      
    ServiceApp().run()


来源:https://stackoverflow.com/questions/22263752/python-showing-a-web-browser-iframe-right-into-the-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!