ironmantis7x
ironmantis7x

Reputation: 827

how to open a tab in a web browser using python

I am developing a web browser in python and Iwant to add tabs to it. I cannot figure out how to do it.

my current code for the browser tab functionality is this:

#!/usr/bin/env python
import gtk, webkit
import webbrowser

#WOW
class visionbrowser():

    def __init__(self):
        # Create window
        self.much_window = gtk.Window()
        #self.much_window.set_icon_from_file('mAILogo3p.png')
        self.much_window.connect('destroy', lambda w: gtk.main_quit())
        self.much_window.set_default_size(1600, 1200)

        # Create navigation bar
        self.so_navigation = gtk.HBox()

        self.many_back = gtk.ToolButton(gtk.STOCK_GO_BACK)
        self.such_forward = gtk.ToolButton(gtk.STOCK_GO_FORWARD)
        self.very_refresh = gtk.ToolButton(gtk.STOCK_REFRESH)
        self.wow_address_bar = gtk.Entry()
        self.new_tab = gtk.Button()

        self.many_back.connect('clicked', self.go_back)
        self.such_forward.connect('clicked', self.go_forward)
        self.very_refresh.connect('clicked', self.refresh_page)
        self.wow_address_bar.connect('activate', self.load_page)
        self.new_tab.connect('clicked', self.tab_new)

        self.so_navigation.pack_start(self.many_back, False)
        self.so_navigation.pack_start(self.such_forward, False)
        self.so_navigation.pack_start(self.very_refresh, False)
        self.so_navigation.pack_start(self.wow_address_bar)
        self.so_navigation.pack_start(self.new_tab)

        # Create view for webpage
        self.very_view = gtk.ScrolledWindow()
        self.such_webview = webkit.WebView()
        self.such_webview.open('http://google.com')
        self.such_webview.connect('title-changed', self.change_title)
        self.such_webview.connect('load-committed', self.change_url)
        self.very_view.add(self.such_webview)

        # Add everything and initialize
        self.wow_container = gtk.VBox()
        self.wow_container.pack_start(self.so_navigation, False)
        self.wow_container.pack_start(self.very_view)

        self.much_window.add(self.wow_container)
        self.much_window.show_all()
        gtk.main()

    def load_page(self, widget):
        so_add = self.wow_address_bar.get_text()
        if so_add.startswith('http://') or so_add.startswith('https://'):
            self.such_webview.open(so_add)
        else:
            so_add = 'http://' + so_add
            self.wow_address_bar.set_text(so_add)
            self.such_webview.open(so_add)

    def tab_new(self, widget):
        self.open_new_tab()

    def change_title(self, widget, frame, title):
        self.much_window.set_title('[Vision Browser by Maverick A.I.] -->> ' + title)

    def change_url(self, widget, frame):
        uri = frame.get_uri()
        self.wow_address_bar.set_text(uri)

    def go_back(self, widget):
        self.such_webview.go_back()

    def go_forward(self, widget):
        self.such_webview.go_forward()

    def refresh_page(self, widget):
        self.such_webview.reload()

wow = visionbrowser()

The browser does work and open and I can open the web pages and all is well. However, when Iclick on my tab button to add a new tab, Iget tghis error:

self.open_new_tab()
AttributeError: visionbrowser instance has no attribute 'open_new_tab'

Is there a way to open a tab using gtk or python webbrowser? if so - what is the best and most correct way to implement it in my code above

NOTE: I have tried python webbrowser (import webbrowser) and it just throws an error saying that open_tab is now a recognized gtk command.

Thanks.

Upvotes: 0

Views: 1229

Answers (1)

sawim
sawim

Reputation: 1082

First of all, your visionbrowser class does not have open_new_tab method. Secondly you are using wrong method. There is

webbrowser.open_new_tab("https://docs.python.org/2/library/webbrowser.html")

or

 webbrowser.open_new("https://docs.python.org/2/library/webbrowser.html")

Upvotes: 1

Related Questions