2012-06-17 15 views
6

पर PyGObject के साथ वेबकिट धागे मैं gtk के लिए मुख्य धागे की तुलना में एक अलग थ्रेड पर एक वेबकिट दृश्य लोड करने की कोशिश कर रहा हूं।जीटीके 3

मैं उदाहरण देख PyGTK, Threads and 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 कॉल काम नहीं करता है। कॉल का एकमात्र काम हिस्सा मुख्य धागे पर टिप्पणी की गई है।

उत्तर

6

मुझे 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() 
संबंधित मुद्दे