2012-06-17 32 views
6

Tôi đang cố gắng tải chế độ xem webkit trên một chuỗi khác với chuỗi chính cho gtk.Chủ đề của Webkit với PyGObject trên Gtk3

Tôi thấy ví dụ PyGTK, Threads and WebKit

Tôi hơi sửa đổi để hỗ trợ PyGObject và 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() 

Kết quả đó là một cửa sổ trống rỗng và "sau khi ngủ" in không bao giờ được thực thi. Cuộc gọi idle_add không hoạt động. Phần công việc duy nhất là cuộc gọi được nhận xét về chủ đề chính.

Trả lời

6

Tôi cần GLib.threads_init() trước gdk's.

Giống như sau:

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() 
Các vấn đề liên quan