jueves, 6 de enero de 2011

Create a basic web browser with python+webkit+gtk

Returning to the programming with python, glade and gtknow explain how to make a simple browser using python webkit.

This post is based on Marcelo's blog article, I create interface with glade.



The figure shows the GUI application.


The application simply has a EntryText, the URL where you can enter to load the page when push button go, the page is loaded into the frame with sliders in the center of the interface and has a button  exit. The default is to load a page to start the application.

The application code is shown below:



#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Import gtk and webkit



import gtk
import webkit



    
#class App
class App:
    def __init__(self):
     #function init 
        #bind glade file with Builder object
        self.glade_file = "browser.glade"
        self.glade = gtk.Builder()
        self.glade.add_from_file(self.glade_file)
        #Binding  window with browser
        self.window = self.glade.get_object('browser')
        #Button exit
        self.exit = self.glade.get_object('bexit')
        #Button browser
        self.go = self.glade.get_object('browse')
        #EntryText URL
        self.link = self.glade.get_object('url')
        #Scrolledwindows where you can see a site
        self.scrolledwindow1 = self.glade.get_object('scrolledwindow1')
        #
        #Linking destroy browser with function
        self.window.connect("destroy",self.on_browser_destroy)
        #Linking clicked exit button
        self.exit.connect('clicked', self.on_bexit_clicked)
        #Linking clicked browse button
        self.go.connect('clicked',self.on_browse_clicked)
        #Linking activate with URL entrytext
        self.link.connect('activate',self.on_url_activate)
        #Open  default site
        #Instant class webview 
        self.webview = webkit.WebView()
        #Add webview in scrolledwindow
        self.scrolledwindow1.add(self.webview)
        #Open default page
        self.__go("http://www.debian.org")
        
        #Show window 
        self.window.show_all()
    
    
    def on_url_activate(self,*args):
        #catch url to open page
        url = "http://" + self.enlace.get_text()
        self.__go(url)



    
    def __go(self,url):
        self.webview.open(url)
        
        
    def on_browser_destroy(self,*args):
        #close application
        gtk.main_quit()
    
    def on_bexit_clicked(self,*args):
     #close application
        gtk.main_quit()
    
    def on_browse_clicked(self,*args):
        #Open site
        url = "http://" + self.link.get_text()
        self.__go(url)
    
    def main(self):
     #star application
        gtk.main()


















if __name__ == '__main__':
    app = App()
    app.main()




The following figure shows the browser.



1 comentario: