Skip to content

Commit 3d7670e

Browse files
committed
Add gtk.py example (cztomczak#253), use PyGObject/Gtk, test with Python 2 / 3.
Fix compile.py, allow for execution using "python3" binary. Make ApplicationSettings.log_severity=LOGSEVERITY_ERROR the default, so that annoying X11 server non-fatal errors aren't displayed.
1 parent 75884b6 commit 3d7670e

File tree

5 files changed

+120
-11
lines changed

5 files changed

+120
-11
lines changed

examples/gtk.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Example of embedding CEF Python browser using PyGObject/Gtk library.
2+
# Tested with GTK 3.10.
3+
4+
from cefpython3 import cefpython as cef
5+
# noinspection PyUnresolvedReferences
6+
from gi.repository import GdkX11, Gtk, GObject, GdkPixbuf
7+
import sys
8+
import os
9+
import time
10+
11+
12+
def main():
13+
version_info()
14+
app = GtkExample()
15+
SystemExit(app.run(sys.argv))
16+
17+
18+
def version_info():
19+
print("CEF Python "+cef.__version__)
20+
print("Python "+sys.version[:6])
21+
print("GTK %s.%s" % (Gtk.get_major_version(), Gtk.get_minor_version()))
22+
23+
24+
class GtkExample(Gtk.Application):
25+
26+
def __init__(self):
27+
super(GtkExample, self).__init__(application_id='cefpython.gtk')
28+
self.browser = None
29+
self.window = None
30+
31+
def run(self, argv):
32+
cef.Initialize()
33+
GObject.threads_init()
34+
GObject.timeout_add(10, self.on_timer)
35+
self.connect("activate", self.on_activate)
36+
self.connect("shutdown", self.on_shutdown)
37+
return super(GtkExample, self).run(argv)
38+
39+
def on_timer(self):
40+
cef.MessageLoopWork()
41+
return True
42+
43+
def on_activate(self, *_):
44+
self.window = Gtk.ApplicationWindow.new(self)
45+
self.window.set_title("Gtk example")
46+
self.window.set_default_size(800, 600)
47+
self.window.connect("configure-event", self.on_configure)
48+
self.window.connect("size-allocate", self.on_size_allocate)
49+
self.window.connect("focus-in-event", self.on_focus_in)
50+
self.window.connect("delete-event", self.on_window_close)
51+
self.setup_icon()
52+
self.window.realize()
53+
window_info = cef.WindowInfo()
54+
window_info.SetAsChild(self.window.get_property("window").get_xid())
55+
self.browser = cef.CreateBrowserSync(window_info,
56+
url="https://www.google.com/")
57+
self.window.get_property("window").focus(False)
58+
self.window.show_all()
59+
60+
def on_configure(self, *_):
61+
if self.browser:
62+
self.browser.NotifyMoveOrResizeStarted()
63+
return False
64+
65+
def on_size_allocate(self, _, data):
66+
if self.browser:
67+
self.browser.SetBounds(data.x, data.y, data.width, data.height)
68+
69+
def on_focus_in(self, *_):
70+
if self.browser:
71+
self.browser.SetFocus(True)
72+
return True
73+
return False
74+
75+
def on_window_close(self, *_):
76+
# Close browser and free reference
77+
self.browser.CloseBrowser(True)
78+
del self.browser
79+
# Give the browser some time to close before calling cef.Shutdown()
80+
for i in range(10):
81+
cef.MessageLoopWork()
82+
time.sleep(0.01)
83+
84+
def on_shutdown(self, *_):
85+
cef.Shutdown()
86+
87+
def setup_icon(self):
88+
icon = os.path.join(os.path.dirname(__file__), "resources", "gtk.png")
89+
if not os.path.exists(icon):
90+
return
91+
pixbuf = GdkPixbuf.Pixbuf.new_from_file(icon)
92+
transparent = pixbuf.add_alpha(True, 0xff, 0xff, 0xff)
93+
Gtk.Window.set_default_icon_list([transparent])
94+
95+
96+
if __name__ == '__main__':
97+
main()

examples/hello_world.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# Hello world example doesn't depend on any third party GUI framework.
1+
# Hello world example. Doesn't depend on any third party GUI framework.
22

33
from cefpython3 import cefpython as cef
44
import sys
55

66

77
def main():
88
"""Main entry point."""
9+
version_info()
910
sys.excepthook = cef.ExceptHook
1011
cef.Initialize()
1112
browser = cef.CreateBrowserSync(url="https://www.google.com/")
@@ -14,6 +15,11 @@ def main():
1415
cef.Shutdown()
1516

1617

18+
def version_info():
19+
print("CEF Python "+cef.__version__)
20+
print("Python "+sys.version[:6])
21+
22+
1723
class ClientHandler:
1824

1925
def OnBeforeClose(self, browser):

examples/resources/gtk.png

7.93 KB
Loading

src/cefpython.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,10 @@ def Initialize(applicationSettings=None, commandLineSwitches=None):
602602

603603
if "debug" not in applicationSettings:
604604
applicationSettings["debug"] = False
605+
if "log_severity" not in applicationSettings:
606+
# By default show only errors. Don't show on Linux X server non-fatal
607+
# errors like "WARNING:x11_util.cc(1409)] X error received".
608+
applicationSettings["log_severity"] = LOGSEVERITY_ERROR
605609
if "string_encoding" not in applicationSettings:
606610
applicationSettings["string_encoding"] = "utf-8"
607611
if "unique_request_context_per_browser" not in applicationSettings:

src/linux/compile.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@
153153

154154
os.chdir("./setup")
155155

156-
ret = subprocess.call("python fix_pyx_files.py", shell=True)
156+
ret = subprocess.call("{python} fix_pyx_files.py"
157+
.format(python=sys.executable), shell=True)
157158
if ret != 0:
158159
sys.exit("ERROR")
159160

@@ -168,11 +169,11 @@
168169
" --cython-gdb", shell=True)
169170
else:
170171
if FAST:
171-
ret = subprocess.call("python setup.py build_ext --inplace --fast",
172-
shell=True)
172+
ret = subprocess.call("{python} setup.py build_ext --inplace --fast"
173+
.format(python=sys.executable), shell=True)
173174
else:
174-
ret = subprocess.call("python setup.py build_ext --inplace",
175-
shell=True)
175+
ret = subprocess.call("{python} setup.py build_ext --inplace"
176+
.format(python=sys.executable), shell=True)
176177

177178
if DEBUG:
178179
shutil.rmtree("./../binaries_%s/cython_debug/" % BITS, ignore_errors=True)
@@ -202,7 +203,8 @@
202203
subprocess.call("cygdb . --args python-dbg wxpython.py", shell=True)
203204
else:
204205
if KIVY:
205-
os.system("python binaries_64bit/kivy_.py")
206+
os.system("{python} binaries_64bit/kivy_.py"
207+
.format(python=sys.executable))
206208
else:
207209
print("Make installer and run setup.py install...")
208210

@@ -220,10 +222,10 @@
220222
sudo = ""
221223

222224
# Make installer, install, run hello world and return to initial dir
223-
os.system("cd ./installer/ && python make-setup.py --version {ver}"
225+
os.system("cd ./installer/ && {python} make-setup.py --version {ver}"
224226
" && cd cefpython3-{ver}-*-setup/"
225-
" && {sudo} python setup.py install"
227+
" && {sudo} {python} setup.py install"
226228
" && cd ../ && {sudo} rm -rf ./cefpython3-{ver}-*-setup/"
227229
" && cd ../../../examples/"
228-
" && python hello_world.py && cd ../src/linux/"
229-
.format(ver=VERSION, sudo=sudo))
230+
" && {python} hello_world.py && cd ../src/linux/"
231+
.format(python=sys.executable, ver=VERSION, sudo=sudo))

0 commit comments

Comments
 (0)