-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample4_webkitgtk.py
More file actions
executable file
·139 lines (108 loc) · 4.68 KB
/
example4_webkitgtk.py
File metadata and controls
executable file
·139 lines (108 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python3
# A port of [^1] from PyGTK to PyGObject + GTK 3.0.
# Original author: Andy Breiner <breinera@gmail.com>
# Author: Ratchanan Srirattanamet <peathot@hotmail.com>
#
# Note: the listed license at [^1] specify the license simply as "GPL", without
# clarifying which versions of GPL are applicable. The code is being used
# as a demonstration only, which is _probably_ fine, but don't try to push
# your luck on this file's license.
#
# [^1]: http://www.eurion.net/python-snippets/snippet/Webkit%20Browser.html
import gi
gi.require_versions(
{
"Gtk": "3.0",
"WebKit2": "4.1",
}
)
from gi.repository import Gio, Gtk, WebKit2
class Browser:
default_site = "http://th.pycon.org/"
def delete_event(self, widget, event, data=None):
return False
def destroy(self, widget, data=None):
self.app.quit()
def __init__(self):
self.app = Gtk.Application.new(
"xyz.peat-network.webkitexample", Gio.ApplicationFlags.DEFAULT_FLAGS
)
self.app.connect("activate", self.app_activated)
def app_activated(self, app):
self.window = Gtk.ApplicationWindow.new(self.app)
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
self.web_view = WebKit2.WebView.new()
self.web_view.load_uri(self.default_site)
toolbar = Gtk.Toolbar.new()
# create the back button and connect the action to
# allow us to go backwards using webkit
self.back_button = Gtk.ToolButton(stock_id=Gtk.STOCK_GO_BACK)
self.back_button.connect("clicked", self.go_back)
# same idea for forward button
self.forward_button = Gtk.ToolButton(stock_id=Gtk.STOCK_GO_FORWARD)
self.forward_button.connect("clicked", self.go_forward)
# again for refresh
refresh_button = Gtk.ToolButton(stock_id=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-changed", self.load_changed)
scroll_window = Gtk.ScrolledWindow(hadjustment=None, vadjustment=None)
scroll_window.add(self.web_view)
hbox = Gtk.HBox(homogeneous=False, spacing=0)
hbox.pack_start(toolbar, False, False, 0)
hbox.pack_start(self.url_bar, True, True, 0)
vbox = Gtk.VBox(homogeneous=False, spacing=0)
vbox.pack_start(hbox, 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.load_uri(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 load_changed(self, webview, load_event):
if load_event == WebKit2.LoadEvent.COMMITTED:
self.update_buttons()
def update_buttons(self):
"""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(self.web_view.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):
self.app.run()
if __name__ == "__main__":
browser = Browser()
browser.main()