Skip to content

Commit 78b5f8d

Browse files
committed
Added PyGTK example for Linux - it works! wxPython example
is not yet complete.
1 parent 02af2f2 commit 78b5f8d

5 files changed

Lines changed: 412 additions & 1 deletion

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset=utf-8>
5+
<title>CefSimple (utf-8: ąś)</title>
6+
<style>
7+
body { font: 13px Segoe UI, Arial; line-height: 1.4em; }
8+
pre { background: #ddd; font: 12px Consolas, Courier New; }
9+
</style>
10+
</head>
11+
<body>
12+
13+
Welcome to <b>CEF Python</b> - bindings for the Chromium Embedded Framework.<br><br>
14+
15+
Project's website: <a href="http://code.google.com/p/cefpython/">http://code.google.com/p/cefpython/</a><br>
16+
Wiki documentation: <a href="http://code.google.com/p/cefpython/wiki/">http://code.google.com/p/cefpython/wiki/</a><br>
17+
Support forum: <a href="https://groups.google.com/group/cefpython?hl=en">https://groups.google.com/group/cefpython?hl=en</a><br>
18+
19+
<h3>Google Search</h3>
20+
21+
<a href="http://www.google.com/">http://www.google.com/</a>
22+
23+
<h3>User agent</h3>
24+
25+
<script>document.write(navigator.userAgent)</script>
26+
27+
<h3>Popup</h3>
28+
29+
<a href="javascript:window.open('cefsimple.html')">
30+
window.open('cefsimple.html')</a>
31+
32+
<br><br><br><br><br><br><br><br><br><br><br>
33+
<br><br><br><br><br><br><br><br><br><br><br>
34+
<br><br><br><br><br><br><br><br><br><br><br>
35+
36+
</body>
37+
</html>
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# An example of embedding CEF in PyGTK application.
2+
3+
import platform
4+
if platform.architecture()[0] != "32bit":
5+
raise Exception("Only 32bit architecture is supported")
6+
7+
import ctypes, os
8+
ctypes.CDLL(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'libcef.so'), ctypes.RTLD_GLOBAL)
9+
import cefpython_py27 as cefpython
10+
11+
import sys
12+
13+
"""
14+
try:
15+
# Import local PYD file (portable zip).
16+
if sys.hexversion >= 0x02070000 and sys.hexversion < 0x03000000:
17+
import cefpython_py27 as cefpython
18+
elif sys.hexversion >= 0x03000000 and sys.hexversion < 0x04000000:
19+
import cefpython_py32 as cefpython
20+
else:
21+
raise Exception("Unsupported python version: %s" % sys.version)
22+
except ImportError:
23+
# Import from package (installer).
24+
from cefpython1 import cefpython
25+
"""
26+
27+
import pygtk
28+
pygtk.require('2.0')
29+
import gtk
30+
import gobject
31+
import ctypes
32+
import re
33+
34+
def GetApplicationPath(file=None):
35+
import re, os
36+
# If file is None return current directory without trailing slash.
37+
if file is None:
38+
file = ""
39+
# Only when relative path.
40+
if not file.startswith("/") and not file.startswith("\\") and (
41+
not re.search(r"^[\w-]+:", file)):
42+
if hasattr(sys, "frozen"):
43+
path = os.path.dirname(sys.executable)
44+
elif "__file__" in globals():
45+
path = os.path.dirname(os.path.realpath(__file__))
46+
else:
47+
path = os.getcwd()
48+
path = path + os.sep + file
49+
if platform.system() == "Windows":
50+
path = re.sub(r"[/\\]+", re.escape(os.sep), path)
51+
path = re.sub(r"[/\\]+$", "", path)
52+
return path
53+
return str(file)
54+
55+
def ExceptHook(type, value, traceObject):
56+
import traceback, os, time
57+
# This hook does the following: in case of exception display it,
58+
# write to error.log, shutdown CEF and exit application.
59+
error = "\n".join(traceback.format_exception(type, value, traceObject))
60+
with open(GetApplicationPath("error.log"), "a") as file:
61+
file.write("\n[%s] %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), error))
62+
print("\n"+error+"\n")
63+
cefpython.QuitMessageLoop()
64+
cefpython.Shutdown()
65+
# So that "finally" does not execute.
66+
os._exit(1)
67+
68+
class PyGTKExample:
69+
70+
mainWindow = None
71+
container = None
72+
browser = None
73+
exiting = None
74+
searchEntry = None
75+
vbox = None
76+
77+
def __init__(self):
78+
79+
self.mainWindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
80+
self.mainWindow.connect('destroy', self.OnExit)
81+
self.mainWindow.set_size_request(width=600, height=400)
82+
self.mainWindow.set_title('PyGTK CEF example')
83+
self.mainWindow.realize()
84+
85+
self.vbox = gtk.VBox(False, 0)
86+
self.vbox.pack_start(self.CreateMenu(), False, False, 0)
87+
self.mainWindow.add(self.vbox)
88+
89+
m = re.search("GtkVBox at 0x(\w+)", str(self.vbox))
90+
hexID = m.group(1)
91+
windowID = int(hexID, 16)
92+
93+
windowInfo = cefpython.WindowInfo()
94+
windowInfo.SetAsChild(windowID)
95+
# Linux requires adding "file://" for local files,
96+
# otherwise /home/some will be replaced as http://home/some
97+
self.browser = cefpython.CreateBrowserSync(
98+
windowInfo,
99+
browserSettings={},
100+
navigateUrl="file://"+GetApplicationPath("cefsimple.html"))
101+
102+
# Must be show_all() for VBox otherwise browser doesn't
103+
# appear when you just call show().
104+
self.vbox.show_all()
105+
106+
self.mainWindow.show()
107+
gobject.timeout_add(10, self.OnTimer)
108+
109+
def CreateMenu(self):
110+
111+
file = gtk.MenuItem('File')
112+
file.show()
113+
filemenu = gtk.Menu()
114+
item = gtk.MenuItem('Open')
115+
filemenu.append(item)
116+
item.show()
117+
item = gtk.MenuItem('Exit')
118+
filemenu.append(item)
119+
item.show()
120+
file.set_submenu(filemenu)
121+
122+
about = gtk.MenuItem('About')
123+
about.show()
124+
aboutmenu = gtk.Menu()
125+
item = gtk.MenuItem('CEF Python')
126+
aboutmenu.append(item)
127+
item.show()
128+
about.set_submenu(aboutmenu)
129+
130+
menubar = gtk.MenuBar()
131+
menubar.append(file)
132+
menubar.append(about)
133+
menubar.show()
134+
135+
return menubar
136+
137+
def OnWidgetClick(self, widget, data):
138+
139+
self.mainWindow.get_window().focus()
140+
141+
def OnTimer(self):
142+
143+
if self.exiting:
144+
return False
145+
cefpython.MessageLoopWork()
146+
return True
147+
148+
def OnFocusIn(self, widget, data):
149+
150+
# This function is currently not called by any of code,
151+
# but if you would like for browser to have automatic focus
152+
# add such line:
153+
# self.mainWindow.connect('focus-in-event', self.OnFocusIn)
154+
self.browser.SetFocus(True)
155+
156+
def OnExit(self, widget, data=None):
157+
158+
self.exiting = True
159+
gtk.main_quit()
160+
161+
if __name__ == '__main__':
162+
163+
version = '.'.join(map(str, list(gtk.gtk_version)))
164+
print('GTK version: %s' % version)
165+
166+
sys.excepthook = ExceptHook
167+
cefpython.g_debug = True
168+
cefpython.g_debugFile = GetApplicationPath("debug.log")
169+
settings = {
170+
"log_severity": cefpython.LOGSEVERITY_INFO,
171+
"log_file": GetApplicationPath("debug.log"),
172+
"release_dcheck_enabled": True, # Enable only when debugging.
173+
# This directories must be set on Linux
174+
"locales_dir_path": GetApplicationPath("locales"),
175+
"resources_dir_path": GetApplicationPath()
176+
}
177+
cefpython.Initialize(settings)
178+
179+
gobject.threads_init() # timer for messageloop
180+
PyGTKExample()
181+
gtk.main()
182+
183+
cefpython.Shutdown()

0 commit comments

Comments
 (0)