Python - 在应用程序中显示Web浏览器/ iframe

时间:2014-03-08 01:23:01

标签: python iframe browser kivy

我有一个脚本,如果对回答问题的人有帮助,就是使用kivy。 我想让它在运行时显示一个iframe类型的东西,而不是打开 浏览器。例如:

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

显然这不起作用,因为python不是html。请记住,我不是想跑 这个脚本在网上,但在kivy发射器上。按照预期,它不应该打开 webbrowser,而是在内置于脚本中的框中显示页面。

2 个答案:

答案 0 :(得分:3)

你试图在Android设备上这样做?目前还没有构建方法,但我很确定可以通过pyjnius加载本机android webview。我不确定当前的状态,但例如here是如何做到这一点的一个例子。我已经粘贴了下面的一些代码,但如果您有任何问题,我建议您在kivy邮件列表或irc上询问,因为这种事情正在讨论和开发中。

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)

答案 1 :(得分:3)

这是一个实际运行的例子,它可以在&#34; Kivy Launcher&#34;应用程式:

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