GTK3 WebKit python3 пример простейшего браузера

Автор: Igor Kirsanov

Пример использования браузера в GUI на GTK3. Синтаксис python 3.

Окно, заголовок, кнопка вернуться на предыдущую страницу.


#!/usr/bin/env python3

import gi
# May or may not be needed :
#gi.require_version('WebKit', '3.0')

# "WebKit" NOT "webkit" :
from gi.repository import Gtk,WebKit

def goback(button):
   view.go_back()

def navrequest(thisview, frame, networkRequest):
   address = networkRequest.get_uri()
   if not "debian.org" in address:
   # GTK2 style:
   #   md = Gtk.MessageDialog(win, Gtk.DIALOG_DESTROY_WITH_PARENT, Gtk.MESSAGE_INFO, Gtk.BUTTONS_CLOSE, "Not allowed to leave the site!")
   # GTK3 style :
   # (however I don't know about " Gtk.DIALOG_DESTROY_WITH_PARENT" ?)
      md = Gtk.MessageDialog(win,0,Gtk.MessageType.INFO, Gtk.ButtonsType.CLOSE, "Not allowed to leave the site!")

      md.run()
      md.destroy()
      view.open("http://www.debian.org")

view = WebKit.WebView()
view.connect("navigation-requested", navrequest)

sw = Gtk.ScrolledWindow()
sw.add(view)

button = Gtk.Button("Back")
button.connect("clicked", goback)

vbox = Gtk.VBox()
vbox.pack_start(button, False, False, 0)
vbox.add(sw)

#GTK2 style
#win = Gtk.Window(Gtk.WINDOW_TOPLEVEL)
#GTK3 style:
win = Gtk.Window()
win.set_size_request(800, 600)
win.connect("destroy", Gtk.main_quit)
win.set_title("Linux Voice browser")
win.add(vbox)
win.show_all()

view.open("http://www.debian.org")
Gtk.main()