Ir al contenido principal

Hi, I'm Mariano Guerra, below is my blog, if you want to learn more about me and what I do check a summary here: marianoguerra.github.io or find me on twitter @warianoguerra or Mastodon @marianoguerra@hachyderm.io

Browser en pygtk con webkit en unas cuantas lineas de codigo


import sys
import gtk
# https://bugs.launchpad.net/bugs/480398
gtk.gdk.threads_init()
import webkit

class Browser(gtk.Window):
def __init__(self, url=''):
gtk.Window.__init__(self)
self.set_title('Browser')
self.set_default_size(640, 480)

self.url = url

scroll = gtk.ScrolledWindow()
scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
scroll.set_shadow_type(gtk.SHADOW_IN)

self.view = webkit.WebView()
scroll.add(self.view)

vbox = gtk.VBox()

entry = gtk.Entry()
entry.connect('activate', self._on_url_changed)

if self.url:
entry.set_text(self.url)
entry.activate()

vbox.pack_start(entry, False)
vbox.pack_start(scroll, True, True)
vbox.show_all()

self.add(vbox)

self.connect('delete-event', lambda *args: sys.exit(0))

def _on_url_changed(self, entry):
'''called when the url changes'''
self.url = entry.get_text()

if not self.url.startswith('http://') and not self.url.startswith('https://'):
self.url = 'http://' + self.url

self.view.open(self.url)

if __name__ == '__main__':
browser = Browser('www.google.com')
browser.show()
gtk.main()