[pygtk] Images in gtkhtml2

Christian Becke christianbecke at web.de
Fri Feb 9 04:58:24 WST 2007


Am Donnerstag, den 08.02.2007, 00:19 +0000 schrieb Christopher
Backhouse:
> I am trying to use gtkhtml, despite the complete lack of any 
> documentation I managed to get it working. But images don't work - in 
> the code below the image comes up with the blank image icon. The same 
> html renders fine in firefox. Any ideas?

When images etc. are found in a HTML document,
gtkhtml2.Document fires a 'request-url' signal with the document, the
image url and a stream handle as parameters.
You have to connect a callback to this signal which reads data from the
given url and writes it to the stream handle, e.g.:

import gtk, gtkhtml2, gnomevfs

def request_url (doc, url, stream):
	try:
		f = gnomevfs.open (url, gnomevfs.OPEN_READ)
	except:
		print "Error opening url", url
		stream.close ()
		return

	while 1:
		try:
			stream.write (f.read (1024))
		except gnomevfs.EOFError:
			break
	f.close ()
	stream.close ()

doc=gtkhtml2.Document()
doc.connect ('request-url', request_url)

doc.open_stream("text/html")
doc.write_stream('text <img src="file:///tmp/test.jpg"> text2')
doc.close_stream()
view=gtkhtml2.View()
view.set_document(doc)
win=gtk.Window()
win.connect ('delete-event', gtk.main_quit)
win.add(view)
win.show_all()
gtk.main()


HTH,

Christian




More information about the pygtk mailing list