Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    gtkhtml2 widget in python

    Reply
    Slocan | # 1 | 2010-01-03, 03:39 | Report

    I had a lot of trouble finding information on how to display a bit of HTML in my python/gtk application (the webkit module for QT application makes it way easier and nicer than in gtk, but QT doesn't seem to have the hildon widgets readily available quite yet). So, after a lot of looking around, here is a small example on how to use gtkhtml2 with finger scrolling, on Maemo Fremantle. One little gripe that remains is that it does text selection at the same time as scrolling.

    Code:
    import hildon
    import gtk
    import gtkhtml2
    import urllib2
    
    class MyApp:
        def __init__(self, html):        
            # Create the main window
            self.window = hildon.StackableWindow()  
            
            # Create the GtkHtml View and Document.
            # gtkhtml2.View is a GtkWidget, while gtkhtml2.Document deals with the html content
            self.view = gtkhtml2.View()
            self.document = gtkhtml2.Document()
            self.view.set_document(self.document)
            
            # Create a PannableArea, to allow finger scrolling
            self.pannableArea = hildon.PannableArea()
            # Add the gtkhtml2.View to the PannableArea. Do NOT use .add_with_viewport
            self.pannableArea.add(self.view)
            # Tell the PannableArea to scroll in both directions (only horizontal by default)
            self.pannableArea.set_property("mov-mode", hildon.MOVEMENT_MODE_BOTH)
            
            # Attach to the signals from the gtkhtml
            # link-clicked is triggered when a link is clicked on the HTML page
            self.document.connect("link_clicked", self._signal_link_clicked, self.document)
            # request-url is triggered when the gtkhtml2 object tries to load a url, in particular images
            self.document.connect("request-url", self._signal_request_url)
            
            # Add all that to the window
            self.window.add(self.pannableArea)
            
            # Load the actual document
            self.load_html(self.document, html)
            
            # Display everything
            self.window.show_all()
    
        def load_html(self, document, html):
            # Load the html data into a gtkhtml2.Document
            # Clear the current content
            document.clear()
            # Open a stream, of the type "text/html" (other content types are possible)
            document.open_stream("text/html")
            # Write the html data into the stream
            document.write_stream(html)
            # Close the stream
            document.close_stream()
        
        def _signal_link_clicked(self, object, link, document):
            # Called when a link is clicked. Fetch the URL with urllib2
            f = urllib2.urlopen(link)
            html = f.read()
            f.close()
            # Load the fetched data in the gtkhtml2.Document
            self.load_html(document, html)
    
        def _signal_request_url(self, object, url, stream):
            # This function is called in particular for linked images on a page
            # Fetch the image, and write it into the stream provided
            # We first check it is a full URL. Relative URL would need more processing
            if (url.lower().startswith("http")):
                f = urllib2.urlopen(url)
                stream.write(f.read())
            stream.close()
            
        def run(self):
            # Main loop
            self.window.connect("destroy", gtk.main_quit)
            gtk.main()
    
    if __name__ == "__main__":
        html = '''<a href="http://www.google.com">Link</a><BR />
    Text<BR />And More Text<BR />'''
        app = MyApp(html)
        app.run()

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following 3 Users Say Thank You to Slocan For This Useful Post:
    mbo, qwerty12, ymb

     
    mbo | # 2 | 2010-01-03, 19:17 | Report

    Thanks, that's exactly what i was looking for!

    But there is a problem:

    "link_clicked" doesn't seem to be triggered in my script. Even in your example.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    Slocan | # 3 | 2010-01-03, 22:14 | Report

    Hmm, it works for me, both in scratchbox and on the n900.
    If you start it from the shell, are you getting any error messages in there?

    Edit | Forward | Quote | Quick Reply | Thanks

     
    mbo | # 4 | 2010-01-04, 11:28 | Report

    ok, was my fault

    The links are extremely difficult to hit.
    At random it works to click one, but I don't know how it happened.

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by mbo; 2010-01-04 at 11:29. Reason: typo

     
    Slocan | # 5 | 2010-01-04, 20:29 | Report

    Yes, it is quite tricky to hit the links right. Using the stylus or finger nails are a must. But it is not made to be a full featured web-browser, it's just to display a bit of HTML formatting. If you're generating the HTML yourself, using images for links might make it easier to click.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    mbo | # 6 | 2010-01-06, 10:21 | Report

    I'm displaying a search result list, so images are no option for me.

    I switched to hildon.TouchSelector, which works quite well for my app.

    Edit | Forward | Quote | Quick Reply | Thanks

     
vBulletin® Version 3.8.8
Normal Logout