| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 75884b6 commit 3d7670e
5 files changed
| Original file line number | Diff line number | Diff 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() | ||
| Original file line number | Diff line number | Diff 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. | ||
| 2 | 2 | ||
| 3 | 3 | from cefpython3 import cefpython as cef | |
| 4 | 4 | import sys | |
| 5 | 5 | ||
| 6 | 6 | ||
| 7 | 7 | def main(): | |
| 8 | 8 | """Main entry point.""" | |
| 9 | + version_info() | ||
| 9 | 10 | sys.excepthook = cef.ExceptHook | |
| 10 | 11 | cef.Initialize() | |
| 11 | 12 | browser = cef.CreateBrowserSync(url="https://www.google.com/") | |
@@ -14,6 +15,11 @@ def main(): | |||
| 14 | 15 | cef.Shutdown() | |
| 15 | 16 | ||
| 16 | 17 | ||
| 18 | + def version_info(): | ||
| 19 | + print("CEF Python "+cef.__version__) | ||
| 20 | + print("Python "+sys.version[:6]) | ||
| 21 | + | ||
| 22 | + | ||
| 17 | 23 | class ClientHandler: | |
| 18 | 24 | ||
| 19 | 25 | def OnBeforeClose(self, browser): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -602,6 +602,10 @@ def Initialize(applicationSettings=None, commandLineSwitches=None): | |||
| 602 | 602 | ||
| 603 | 603 | if "debug" not in applicationSettings: | |
| 604 | 604 | 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 | ||
| 605 | 609 | if "string_encoding" not in applicationSettings: | |
| 606 | 610 | applicationSettings["string_encoding"] = "utf-8" | |
| 607 | 611 | if "unique_request_context_per_browser" not in applicationSettings: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -153,7 +153,8 @@ | |||
| 153 | 153 | ||
| 154 | 154 | os.chdir("./setup") | |
| 155 | 155 | ||
| 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) | ||
| 157 | 158 | if ret != 0: | |
| 158 | 159 | sys.exit("ERROR") | |
| 159 | 160 | ||
@@ -168,11 +169,11 @@ | |||
| 168 | 169 | " --cython-gdb", shell=True) | |
| 169 | 170 | else: | |
| 170 | 171 | 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) | ||
| 173 | 174 | 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) | ||
| 176 | 177 | ||
| 177 | 178 | if DEBUG: | |
| 178 | 179 | shutil.rmtree("./../binaries_%s/cython_debug/" % BITS, ignore_errors=True) | |
@@ -202,7 +203,8 @@ | |||
| 202 | 203 | subprocess.call("cygdb . --args python-dbg wxpython.py", shell=True) | |
| 203 | 204 | else: | |
| 204 | 205 | if KIVY: | |
| 205 | - os.system("python binaries_64bit/kivy_.py") | ||
| 206 | + os.system("{python} binaries_64bit/kivy_.py" | ||
| 207 | + .format(python=sys.executable)) | ||
| 206 | 208 | else: | |
| 207 | 209 | print("Make installer and run setup.py install...") | |
| 208 | 210 | ||
@@ -220,10 +222,10 @@ | |||
| 220 | 222 | sudo = "" | |
| 221 | 223 | ||
| 222 | 224 | # 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}" | ||
| 224 | 226 | " && cd cefpython3-{ver}-*-setup/" | |
| 225 | - " && {sudo} python setup.py install" | ||
| 227 | + " && {sudo} {python} setup.py install" | ||
| 226 | 228 | " && cd ../ && {sudo} rm -rf ./cefpython3-{ver}-*-setup/" | |
| 227 | 229 | " && 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)) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments