​

Thursday, 4 May 2017

Create a Simple Browser Using PyGTK GLADE And MozEmbed

Create a Simple Browser Using PyGTK GLADE And MozEmbed


Lets start creating softwares using Python GTK (PyGTK). Python is the easiest programming language ever and If you are coming from other Programming Languages such as PHP, Here is something that you should understand :
  1. Python doesnt have semi-colons for closing a code.
  2. function in PHP is def in Python.
  3. Spaces from the starting of a line decides the wrapper of the code.
  4. { in PHP is : in Python.
  5. There is no closing } in Python.
  6. Since there is no closing attribute (";"), 2 or more code cant be placed in a single line.
  7. Python doesnt need variables starting with $.
The Browser we are going to create is built using GTK 2. The latest version of GTK is GTK 3.
We are going to use GLADE for designing the interface. If you dont have GLADE installed, install it using the following command :
sudo apt-get install glade
Lets begin. First of all create a folder named browser on your home folder.
Create a new project in GLADE with the Project File Format as GtkBuilder :
Save the project as window.glade in browser folder we created in home folder.
Create a Window by clicking on Window button in Toplevels section on the toolbox. Choose Signals and add remove signal of GtkContainer to on_MainWindow_close_event :

Choose Vertical Box from the Containers and click on the blank space of the window1. The Number Of Items should be 2. The window1 would be divided into 2 now.

Choose Tool Bar from the Containers and click on the 1st division of window1.
Right click on toolbar1 and choose Edit option. Toggle to Hierarchy tab and add the following Buttons :
Name            :new
Label:New
Icon Name:list-add
Name            :stop
Label:Stop
Icon Name:view-refresh
Name            :reload
Label:Reload
Icon Name:edit-delete
Name            :urlcont
Type:Custom
Expand:Yes
Choose Text Entry from Control and Display and click on empty space of urlcont of toolbar1.
The Current Window Will Look Like This:
The Interfacing is over, Now we should create the Python file. Create a file named browser.py in the directory containing window.glade. Add The Following contents on browser.py :
#!/usr/bin/python
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
import gtkmozembed
class SubinsWindow:
 def __init__(self):
  self.gladefile = "window.glade"
  self.glade = gtk.Builder()
  self.glade.add_from_file(self.gladefile)
  self.glade.connect_signals(self)
  self.win=self.glade.get_object("window1")
  self.win.show_all()
  self.win.maximize()
  self.win.set_title("Subins Blog Browser - http://www.subinsb.com")
  self.mw = gtkmozembed.MozEmbed()
  self.glade.get_object("vbox1").add(self.mw)
  self.mw.set_size_request(800,600)
  self.mw.load_url("http://www.subinsb.com")
  self.mw.show()
  self.glade.get_object("entry1").set_text("http://www.subinsb.com")
  self.glade.get_object("entry1").connect("activate",self.on_entry1_activate)
  self.glade.get_object("reload").connect("clicked",self.on_entry1_activate)
  self.glade.get_object("stop").connect("clicked",self.on_stop_click)
  self.glade.get_object("new").connect("clicked",self.on_new_click)

 def on_entry1_activate(self,widget):
  v=self.glade.get_object("entry1").get_text()
  self.mw.load_url(v)
  self.win.set_title("Subins Blog Browser - "+v)

 def on_reload_click(self,widget):
  self.mw.reload()

 def on_new_click(self,widget):
  self.glade.get_object("entry1").set_text("http://www.subinsb.com")
  self.mw.load_url("http://www.subinsb.com")

 def on_stop_click(self,widget):
  v=self.mw.get_location()
  self.mw.stop_load()
  self.win.set_title("Subins Blog Browser - "+v+" - Stopped")

 def on_MainWindow_close_event(self, widget, event=1):
  gtk.main_quit()

if __name__ == "__main__":
 a = SubinsWindow()
 gtk.main()
Thats It ! Run browser.py using python browser.py and see your own Browser in action :
If you have any problems/doubts/suggestions/feedback print it out in the comments. Thank You. Hope You Like it.
download
alternative link download

Like the Post? Do share with your Friends.
​