Author Topic: Lets create a PCLinuxOS webkit browser (python)  (Read 29457 times)

Hootiegibbon

  • Guest
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #30 on: March 29, 2011, 04:42:41 PM »
I'll check out this code and see if I can come up with something. :)


Neal,

Check this out...

Code: [Select]
#!/usr/bin/env python
#
# [SNIPPET_NAME: Webkit Browser]
# [SNIPPET_CATEGORIES: Webkit, PyGTK]
# [SNIPPET_DESCRIPTION: Create a simple browser using the webkit API]
# [SNIPPET_AUTHOR: Andy Breiner <breinera@gmail.com>]
# [SNIPPET_LICENSE: GPL]
# [SNIPPET_DOCS: http://ardoris.wordpress.com/2009/04/26/a-browser-in-14-lines-using-python-and-webkit, http://www.aclevername.com/articles/python-webgui]

import pygtk
pygtk.require('2.0')
import gtk

#you need to import webkit and gobject, gobject is needed for threads
import webkit
import gobject

class Browser:
    default_site = "http://www.pclinuxos.com/forum"

    def delete_event(self, widget, event, data=None):
        return False

    def destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):
        gobject.threads_init()
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_resizable(True)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)

        #webkit.WebView allows us to embed a webkit browser
        #it takes care of going backwards/fowards/reloading
        #it even handles flash
        self.web_view = webkit.WebView()
        self.web_view.open(self.default_site)

        toolbar = gtk.Toolbar()

        #create the back button and connect the action to
        #allow us to go backwards using webkit
        self.back_button = gtk.ToolButton(gtk.STOCK_GO_BACK)
        self.back_button.connect("clicked", self.go_back)

        #same idea for forward button
        self.forward_button = gtk.ToolButton(gtk.STOCK_GO_FORWARD)
        self.forward_button.connect("clicked", self.go_forward)

        #again for refresh
        refresh_button = gtk.ToolButton(gtk.STOCK_REFRESH)
        refresh_button.connect("clicked", self.refresh)

        #add the buttons to the toolbar
        toolbar.add(self.back_button)
        toolbar.add(self.forward_button)
        toolbar.add(refresh_button)

        #entry bar for typing in and display URLs, when they type in a site
        #and hit enter the on_active function is called
        self.url_bar = gtk.Entry()
        self.url_bar.connect("activate", self.on_active)

        #anytime a site is loaded the update_buttons will be called
        self.web_view.connect("load_committed", self.update_buttons)

        scroll_window = gtk.ScrolledWindow(None, None)
        scroll_window.add(self.web_view)
       

        vbox = gtk.VBox(False, 0)
        vbox.pack_start(toolbar, False, True, 0)
        vbox.pack_start(self.url_bar, False, True, 0)
        vbox.add(scroll_window)

        self.window.add(vbox)
        self.window.show_all()

    def on_active(self, widge, data=None):
        '''When the user enters an address in the bar, we check to make
           sure they added the http://, if not we add it for them.  Once
           the url is correct, we just ask webkit to open that site.'''
        url = self.url_bar.get_text()
        try:
            url.index("://")
        except:
            url = "http://"+url
        self.url_bar.set_text(url)
        self.web_view.open(url)

    def go_back(self, widget, data=None):
        '''Webkit will remember the links and this will allow us to go
           backwards.'''
        self.web_view.go_back()

    def go_forward(self, widget, data=None):
        '''Webkit will remember the links and this will allow us to go
           forwards.'''
        self.web_view.go_forward()

    def refresh(self, widget, data=None):
        '''Simple makes webkit reload the current back.'''
        self.web_view.reload()

    def update_buttons(self, widget, data=None):
        '''Gets the current url entry and puts that into the url bar.
           It then checks to see if we can go back, if we can it makes the
           back button clickable.  Then it does the same for the foward
           button.'''
        self.url_bar.set_text( widget.get_main_frame().get_uri() )
        self.back_button.set_sensitive(self.web_view.can_go_back())
        self.forward_button.set_sensitive(self.web_view.can_go_forward())

    def main(self):
        gtk.main()

if __name__ == "__main__":
    browser = Browser()
    browser.main()

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #31 on: March 29, 2011, 04:51:38 PM »
Hey, Jase! You did it! :D :D :D

Hootiegibbon

  • Guest
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #32 on: March 29, 2011, 04:55:05 PM »
Hey, Jase! You did it! :D :D :D

All I can take credit for is being obsessed with this enough to google lots of web pages I think that this is a good starting point.

btw I have had a truly outlandish idea that is left of center even for me... be afraid.... lol

Jase


Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #33 on: March 29, 2011, 05:02:30 PM »
Hey, Jase! You did it! :D :D :D

All I can take credit for is being obsessed with this enough to google lots of web pages I think that this is a good starting point.

btw I have had a truly outlandish idea that is left of center even for me... be afraid.... lol

Jase

PM me. You know me; I'll dive into most any idea.

Offline Sproggy

  • Hero Member
  • *****
  • Posts: 1484
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #34 on: March 29, 2011, 05:04:17 PM »
looking great guys .... well done ;D ;D ;D ;D

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #35 on: March 29, 2011, 05:09:03 PM »
Watching with interest  ;)

What needs to be added to get a bigger initial page?

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #36 on: March 29, 2011, 05:13:12 PM »
Watching with interest  ;)

What needs to be added to get a bigger initial page?

Having a look. :)

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #37 on: March 29, 2011, 05:18:08 PM »
self.window.set_default_size(950, 900)

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #38 on: March 29, 2011, 05:28:06 PM »
LOL I took a moment to grab a bite to eat. Can't work with a woozzy head.
Was going to ask what dimensions, but I see you provided them. :)

Code: [Select]
#!/usr/bin/env python
#
# [SNIPPET_NAME: Webkit Browser]
# [SNIPPET_CATEGORIES: Webkit, PyGTK]
# [SNIPPET_DESCRIPTION: Create a simple browser using the webkit API]
# [SNIPPET_AUTHOR: Andy Breiner <breinera@gmail.com>]
# [SNIPPET_LICENSE: GPL]
# [SNIPPET_DOCS: http://ardoris.wordpress.com/2009/04/26/a-browser-in-14-lines-using-python-and-webkit, http://www.aclevername.com/articles/python-webgui]

import pygtk
pygtk.require('2.0')
import gtk

#you need to import webkit and gobject, gobject is needed for threads
import webkit
import gobject

class Browser:
    default_site = "http://www.pclinuxos.com/forum"

    def delete_event(self, widget, event, data=None):
        return False

    def destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):
        gobject.threads_init()
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_default_size(950, 900)
        self.window.set_resizable(True)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)

        #webkit.WebView allows us to embed a webkit browser
        #it takes care of going backwards/fowards/reloading
        #it even handles flash
        self.web_view = webkit.WebView()
        self.web_view.open(self.default_site)

        toolbar = gtk.Toolbar()

        #create the back button and connect the action to
        #allow us to go backwards using webkit
        self.back_button = gtk.ToolButton(gtk.STOCK_GO_BACK)
        self.back_button.connect("clicked", self.go_back)

        #same idea for forward button
        self.forward_button = gtk.ToolButton(gtk.STOCK_GO_FORWARD)
        self.forward_button.connect("clicked", self.go_forward)

        #again for refresh
        refresh_button = gtk.ToolButton(gtk.STOCK_REFRESH)
        refresh_button.connect("clicked", self.refresh)

        #add the buttons to the toolbar
        toolbar.add(self.back_button)
        toolbar.add(self.forward_button)
        toolbar.add(refresh_button)

        #entry bar for typing in and display URLs, when they type in a site
        #and hit enter the on_active function is called
        self.url_bar = gtk.Entry()
        self.url_bar.connect("activate", self.on_active)

        #anytime a site is loaded the update_buttons will be called
        self.web_view.connect("load_committed", self.update_buttons)

        scroll_window = gtk.ScrolledWindow(None, None)
        scroll_window.add(self.web_view)
       

        vbox = gtk.VBox(False, 0)
        vbox.pack_start(toolbar, False, True, 0)
        vbox.pack_start(self.url_bar, False, True, 0)
        vbox.add(scroll_window)

        self.window.add(vbox)
        self.window.show_all()

    def on_active(self, widge, data=None):
        '''When the user enters an address in the bar, we check to make
           sure they added the http://, if not we add it for them.  Once
           the url is correct, we just ask webkit to open that site.'''
        url = self.url_bar.get_text()
        try:
            url.index("://")
        except:
            url = "http://"+url
        self.url_bar.set_text(url)
        self.web_view.open(url)

    def go_back(self, widget, data=None):
        '''Webkit will remember the links and this will allow us to go
           backwards.'''
        self.web_view.go_back()

    def go_forward(self, widget, data=None):
        '''Webkit will remember the links and this will allow us to go
           forwards.'''
        self.web_view.go_forward()

    def refresh(self, widget, data=None):
        '''Simple makes webkit reload the current back.'''
        self.web_view.reload()

    def update_buttons(self, widget, data=None):
        '''Gets the current url entry and puts that into the url bar.
           It then checks to see if we can go back, if we can it makes the
           back button clickable.  Then it does the same for the foward
           button.'''
        self.url_bar.set_text( widget.get_main_frame().get_uri() )
        self.back_button.set_sensitive(self.web_view.can_go_back())
        self.forward_button.set_sensitive(self.web_view.can_go_forward())

    def main(self):
        gtk.main()

if __name__ == "__main__":
    browser = Browser()
    browser.main()

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #39 on: March 29, 2011, 05:29:26 PM »
The Title seems to be taking the file name here .....  seems that needs to be defined also

Hootiegibbon

  • Guest
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #40 on: March 29, 2011, 05:29:53 PM »
Hey, Jase! You did it! :D :D :D

All I can take credit for is being obsessed with this enough to google lots of web pages I think that this is a good starting point.

btw I have had a truly outlandish idea that is left of center even for me... be afraid.... lol

Jase


I

PM me. You know me; I'll dive into most any idea.


 will get my racing thoughts to calm down and get a PM together and will see if it has legs (to run with..)

Will probably be tomorrow night i PM ya!

Jase

« Last Edit: March 29, 2011, 05:31:56 PM by Hootiegibbon »

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #41 on: March 29, 2011, 05:37:14 PM »
The Title ......

Line 18 here ...  global def
name = "PCLinuxOS Browser"

Line 42  (above  toolbar = gtk.Toolbar()  )
        self.window.set_title('%s' % name)

Gives Title as             PCLinuxOS Browser
« Last Edit: March 29, 2011, 05:39:52 PM by Just19 »

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #42 on: March 29, 2011, 05:49:55 PM »
Jase,
Okay then. LOL I should probably get bit of rest. My eyes are blurring. If only my body could keep up with what I want to do. LOL

Just19,
Why not "PCLinuxOS Hammer?"

I added your code, but the browser no longer launches.

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6849
  • MLU
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #43 on: March 29, 2011, 06:15:04 PM »
My description of placement of line was not good  :(

It needs to be changed to include the current url before the name ......  changed that name for you  ;D  ;D

Code: [Select]
#!/usr/bin/env python
#
# [SNIPPET_NAME: Webkit Browser]
# [SNIPPET_CATEGORIES: Webkit, PyGTK]
# [SNIPPET_DESCRIPTION: Create a simple browser using the webkit API]
# [SNIPPET_AUTHOR: Andy Breiner <breinera@gmail.com>]
# [SNIPPET_LICENSE: GPL]
# [SNIPPET_DOCS: http://ardoris.wordpress.com/2009/04/26/a-browser-in-14-lines-using-python-and-webkit, http://www.aclevername.com/articles/python-webgui]

import pygtk
pygtk.require('2.0')
import gtk

#you need to import webkit and gobject, gobject is needed for threads
import webkit
import gobject

name = "PCLinuxOS Hammer"

class Browser:
    default_site = "http://www.google.ie"

    def delete_event(self, widget, event, data=None):
        return False

    def destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):
        gobject.threads_init()
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_resizable(True)
        self.window.set_default_size(950, 900)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)

        #webkit.WebView allows us to embed a webkit browser
        #it takes care of going backwards/fowards/reloading
        #it even handles flash
        self.web_view = webkit.WebView()
        self.web_view.open(self.default_site)
        self.window.set_title('%s' % name)

        toolbar = gtk.Toolbar()

        #create the back button and connect the action to
        #allow us to go backwards using webkit
        self.back_button = gtk.ToolButton(gtk.STOCK_GO_BACK)
        self.back_button.connect("clicked", self.go_back)

        #same idea for forward button
        self.forward_button = gtk.ToolButton(gtk.STOCK_GO_FORWARD)
        self.forward_button.connect("clicked", self.go_forward)

        #again for refresh
        refresh_button = gtk.ToolButton(gtk.STOCK_REFRESH)
        refresh_button.connect("clicked", self.refresh)

        #add the buttons to the toolbar
        toolbar.add(self.back_button)
        toolbar.add(self.forward_button)
        toolbar.add(refresh_button)

        #entry bar for typing in and display URLs, when they type in a site
        #and hit enter the on_active function is called
        self.url_bar = gtk.Entry()
        self.url_bar.connect("activate", self.on_active)

        #anytime a site is loaded the update_buttons will be called
        self.web_view.connect("load_committed", self.update_buttons)

        scroll_window = gtk.ScrolledWindow(None, None)
        scroll_window.add(self.web_view)
       

        vbox = gtk.VBox(False, 0)
        vbox.pack_start(toolbar, False, True, 0)
        vbox.pack_start(self.url_bar, False, True, 0)
        vbox.add(scroll_window)

        self.window.add(vbox)
        self.window.show_all()

    def on_active(self, widge, data=None):
        '''When the user enters an address in the bar, we check to make
           sure they added the http://, if not we add it for them.  Once
           the url is correct, we just ask webkit to open that site.'''
        url = self.url_bar.get_text()
        try:
            url.index("://")
        except:
            url = "http://"+url
        self.url_bar.set_text(url)
        self.web_view.open(url)

    def go_back(self, widget, data=None):
        '''Webkit will remember the links and this will allow us to go
           backwards.'''
        self.web_view.go_back()

    def go_forward(self, widget, data=None):
        '''Webkit will remember the links and this will allow us to go
           forwards.'''
        self.web_view.go_forward()

    def refresh(self, widget, data=None):
        '''Simple makes webkit reload the current back.'''
        self.web_view.reload()

    def update_buttons(self, widget, data=None):
        '''Gets the current url entry and puts that into the url bar.
           It then checks to see if we can go back, if we can it makes the
           back button clickable.  Then it does the same for the foward
           button.'''
        self.url_bar.set_text( widget.get_main_frame().get_uri() )
        self.back_button.set_sensitive(self.web_view.can_go_back())
        self.forward_button.set_sensitive(self.web_view.can_go_forward())

    def main(self):
        gtk.main()

if __name__ == "__main__":
    browser = Browser()
    browser.main()

ongoto

  • Guest
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #44 on: March 29, 2011, 08:25:01 PM »
Couldn't resist   :)

How would it look if the entry bar was included inside the toolbar?

Code: [Select]
       #entry bar for typing in and display URLs, when they type in a site
        #and hit enter the on_active function is called
        self.url_bar = gtk.Entry()
        self.url_bar.connect("activate", self.on_active)
        entry_tool = gtk.ToolItem()
        entry_tool.set_expand(True)
        entry_tool.add(self.url_bar)
        self.url_bar.show()

        #add the buttons to the toolbar
        toolbar.add(self.back_button)
        toolbar.add(self.forward_button)
        toolbar.add(refresh_button)
        toolbar.add(entry_tool)

Just19
Try
Code: [Select]
self.window.set_title(('%s') % name)

Also, the code you posted above (reply #43) has an ASCII 160 character at the beginning of each indented line, followed by spaces.  It's an unprintable character so it appears to be a space, but it's messing things up for Python.  I don't know if it's your editor doing it or what, but it should be a space (ASCII 32).  Neal's code (reply #38) doesn't show that problem, so I don't think the forum is causing it.



« Last Edit: March 29, 2011, 10:03:38 PM by ongoto »