Keywords: Python; Linux; Unix; 自制浏览器

我应该是一个 newbie 在 python 编程方面,所以完全不好意思在各位大神面前班门弄斧了。
但是由于科研民工都是业余码农,所以在无意中感受到了 python,体会到其完全不同于我日常使用的 Fortran and Matlab。比如:强大的脚本式编译方式(就像在 Linux 系统中写个 bash 脚本), 相较于 C++ 与 Fortran; 强大的 GUI 设计。

心生敬佩之情,难抑激动心情, 不禁抛砖引玉,写个关于用 python 写个简易浏览器的小文章。

Prerequisite

1. 最好你是在使用 Linux or Unix;
2. 安装有 python-webkit;
3. 将下面代码复制进一个叫 Browser.py 文件;


#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
import webkit
import gobject

class Browser:
    default_site = "https://www.google.com";

    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)
        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):
        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):

        self.web_view.go_back()

    def go_forward(self, widget, data=None):

        self.web_view.go_forward()

    def refresh(self, widget, data=None):

        self.web_view.reload()

    def update_buttons(self, widget, data=None):

        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()

最后, 在 Terminal 中运行

 python Browser.py 

然后请君感受下用 python 自制的浏览器。上个 google,然后上个 youtube,然后你发现这么个简易浏览器却也能满足你的日常需要,有没有像我业余码农一样又被刷新了些什么?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s