Question

I'm new to Python, I want open a web page(here is google.com) using pywebkitgtk
then countdown with another thread,
when time's up, send a signal to webview, download the html as file

Is there a way to open a web-page in gtk.main and countdown in background thread, then send a signal to GUI, make GUI do something..

reference material:

  1. Downloading a page’s content with python and WebKit
  2. using a separate thread to run code, two approaches for threads in PyGTK.

here is my code, it cannot run, I guess I do not understand Python's Class...

#!/usr/bin/env python
import sys, threading
import gtk, webkit
import time
import gobject

gobject.threads_init()
google = "http://www.google.com"

class WebView(webkit.WebView):
    #return page's content
    def get_html(self):
        self.execute_script('oldtitle=document.title;document.title=document.documentElement.innerHTML;')
    html = self.get_main_frame().get_title()
    self.execute_script('document.title=oldtitle;')
    return html

#wait 5 senconds and send a signal
class TimeSender(gobject.GObject, threading.Thread):
    def __init__(self):
        self.__gobject_init__()
        threading.Thread.__init__(self)

    def run(self):
        print "sleep 5 seconds"
        time.sleep(5)
        self.emit("Sender_signal")

gobject.type_register(TimeSender)
gobject.signal_new("Sender_signal", TimeSender, gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ())

#PywebkitGTK, open google.com, receive signal
class Window(gtk.Window, gobject.GObject):
    def __init__(self, time_sender, url):
        self.__gobject_init__()
        gtk.Window.__init__(self)
        time_sender.connect('Sender_signal', self._finished_loading)
        self._url = url

    def open_page(self):
        view = WebView()
        view.get_html()
        view.open(self._url)
        self.add(view)
        gtk.main()
    #write html to file
    def _finished_loading(self, view):
        with open("pagehtml.html", 'w') as f:
            f.write(view.get_html())
        gtk.main_quit()

'''
def user_callback(object):
    with open("pagehtml2.html", 'w') as f:
        f.write(view.get_html())
    gtk.main_quit()
'''

if __name__ == '__main__':
    time_sender = TimeSender()
    window = Window(time_sender, google)
    #time_sender.connect("Sender_signal", user_callback)
    time_sender.start()
    window.open_page()

I got an error:
AttributeError: 'TimeSender' object has no attribute 'get_html'
I've been confused for a few days... thanks

Was it helpful?

Solution

Looks like you are confused about singals/objects and threads. _finished_loading method does not get view as a parameter as yo are not passing it. If you make it global it should work. Following piece of code works as expected.

#!/usr/bin/env python
import sys, threading
import gtk, webkit
import time
import gobject

gobject.threads_init()
google = "http://www.google.com"

class WebView(webkit.WebView):
    #return page's content
    def get_html(self):
        self.execute_script('oldtitle=document.title;document.title=document.documentElement.innerHTML;')
        html = self.get_main_frame().get_title()
        self.execute_script('document.title=oldtitle;')
        return html

#wait 5 senconds and send a signal
class TimeSender(gobject.GObject, threading.Thread):
    def __init__(self):
        self.__gobject_init__()
        threading.Thread.__init__(self)

    def myEmit(self):
        window.emit("Sender_signal")

    def run(self):
        print "sleep 5 seconds"
        time.sleep(5)
        gobject.idle_add(self.myEmit)


gobject.type_register(TimeSender)

#PywebkitGTK, open google.com, receive signal
class Window(gtk.Window, gobject.GObject):
    def __init__(self, time_sender, url):
        self.__gobject_init__()
        gtk.Window.__init__(self)
        self.connect('Sender_signal', self._finished_loading)
        self._url = url

    def open_page(self):
        self.view = WebView()
        self.view.get_html()
        self.view.open(self._url)
        self.add(self.view)
        gtk.main()
    #write html to file
    def _finished_loading(self, view1):
        with open("pagehtml.html", 'w') as f:
            f.write(self.view.get_html())
        gtk.main_quit()

'''
def user_callback(object):
    with open("pagehtml2.html", 'w') as f:
        f.write(view.get_html())
    gtk.main_quit()
'''

if __name__ == '__main__':
    gobject.signal_new("Sender_signal", Window, gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ())
    time_sender = TimeSender()
    window = Window(time_sender, google)
    #time_sender.connect("Sender_signal", user_callback)
    time_sender.start()
    window.open_page()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top