繁体   English   中英

Webkit与Gtk3上的PyGObject线程

[英]Webkit threads with PyGObject on Gtk3

我试图在与gtk主线程不同的线程上加载webkit视图。

我看到了示例PyGTK,Threads和WebKit

我稍微修改了支持PyGObject和GTK3:

from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import WebKit
import threading
import time

# Use threads                                       
Gdk.threads_init()

class App(object):
    def __init__(self):
        window = Gtk.Window()
        webView = WebKit.WebView()
        window.add(webView)
        window.show_all()

        #webView.load_uri('http://www.google.com') # Here it works on main thread

        self.window = window
        self.webView = webView

    def run(self):
        Gtk.main()

    def show_html(self):
        print 'show html'

        time.sleep(1)
        print 'after sleep'

        # Update widget in main thread             
        GLib.idle_add(self.webView.load_uri, 'http://www.google.com') # Here it doesn't work

app = App()

thread = threading.Thread(target=app.show_html)
thread.start()

app.run()
Gtk.main()

结果是一个空窗口,“睡眠后”打印永远不会执行。 idle_add调用不起作用。 唯一的工作部分是在主线程上发表评论的电话。

我需要在gdk之前使用GLib.threads_init()。

像这样:

from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import WebKit
import threading
import time

# Use threads                                       
GLib.threads_init()

class App(object):
    def __init__(self):
        window = Gtk.Window()
        webView = WebKit.WebView()
        window.add(webView)
        window.show_all()

        #webView.load_uri('http://www.google.com') # Here it works on main thread

        self.window = window
        self.webView = webView

    def run(self):
        Gtk.main()

    def show_html(self):
        print 'show html'

        time.sleep(1)
        print 'after sleep'

        # Update widget in main thread             
        GLib.idle_add(self.webView.load_uri, 'http://www.google.com') # Here it doesn't work

app = App()

thread = threading.Thread(target=app.show_html)
thread.start()

app.run()
Gtk.main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM