|
| 1 | +# Tutorial (STILL A WORK IN PROGRESS.. #256) |
| 2 | + |
| 3 | +This tutorial is for v50+ versions of CEF Python, which are currently |
| 4 | +available only for Linux. |
| 5 | + |
| 6 | + |
| 7 | +Table of contents: |
| 8 | +* [Install and download examples](#install-and-download-examples) |
| 9 | +* [Hello world](#hello-world) |
| 10 | +* [CEF's multiprocess architecture](#cefs-multiprocess-architecture) |
| 11 | +* [Handling Python exceptions](#handling-python-exceptions) |
| 12 | + |
| 13 | + |
| 14 | +## Install and download examples |
| 15 | + |
| 16 | +The easy way to install CEF Python is through PyPI, using the pip tool, |
| 17 | +which is bundled with all recent versions of Python. On Linux pip 8.1+ |
| 18 | +is required. To check version and install CEF Python type: |
| 19 | + |
| 20 | +``` |
| 21 | +pip --version |
| 22 | +pip install cefpython3 |
| 23 | +``` |
| 24 | + |
| 25 | +Alternatively you can download the setup package from |
| 26 | +[GitHub Releases](../../../releases) and install it by following |
| 27 | +the instructions in README.txt. |
| 28 | + |
| 29 | +Now let's download examples by cloning the GitHub repository. After |
| 30 | +that, enter the "cefpython/examples/" directory. In that directory |
| 31 | +you will find all the examples from this Tutorial, their names |
| 32 | +start with a "tutorial_" prefix, except for the hello world example |
| 33 | +which is just named "hello_world.py". |
| 34 | + |
| 35 | +``` |
| 36 | +git clone https://github.com/cztomczak/cefpython.git |
| 37 | +cd cefpython/examples/ |
| 38 | +``` |
| 39 | + |
| 40 | + |
| 41 | +## Hello world |
| 42 | + |
| 43 | +The [hello_world.py](../../../examples/hello_world.py) example is the |
| 44 | +most basic example. It doesn't depend on any third party GUI frameworks. |
| 45 | +It creates a browser widget which doesn't provide any window information |
| 46 | +(parent window not specified), thus CEF automatically takes care of creating |
| 47 | +a top-level window for us, and in that window a Chromium widget is embedded. |
| 48 | +When creating the browser, an "url" parameter is specified, which causes the |
| 49 | +browser to initially navigate to the Google website. Let's explain the code |
| 50 | +from this example: |
| 51 | + |
| 52 | +1. `from cefpython3 import cefpython as cef` - import the cefpython |
| 53 | + module and bind it to a shorter name "cef" |
| 54 | +2. `sys.excepthook = cef.ExceptHook` - overwrite Python's default |
| 55 | + exception handler so that all CEF processes are terminated when |
| 56 | + Python exception occurs. To understand this better read the |
| 57 | + "CEF's multiprocess architecture" and "Handling Python exceptions" |
| 58 | + sections further down in this Tutorial. |
| 59 | +3. `cef.Initialize()` - Initialize CEF. This function must be called |
| 60 | + somewhere in the beginning of your code. It must be called before |
| 61 | + any app window is created. It must be called only once during app's |
| 62 | + lifetime and must have a corresponding Shutdown() call. |
| 63 | +4. `cef.CreateBrowserSync(url="https://www.google.com/")` - Create |
| 64 | + a browser synchronously, this function returns the Browser object. |
| 65 | +5. `cef.MessageLoop()` - Run CEF message loop. All desktop GUI programs |
| 66 | + run a message loop that waits and dispatches events or messages. |
| 67 | +6. `cef.Shutdown()` - Shut down CEF. This function must be called for |
| 68 | + CEF to shut down cleanly. It will free CEF system resources, it |
| 69 | + will terminate all subprocesses, and it will flush to disk any |
| 70 | + yet unsaved data like for example cookies and other data. Call this |
| 71 | + function at the very end of your program execution. When using third |
| 72 | + party GUI frameworks such as Qt/wxWidgets, CEF should be shut down |
| 73 | + after these frameworks' shutdown procedures were called. For example |
| 74 | + in Qt, shut down CEF only after QApplication object was destroyed. |
| 75 | + |
| 76 | +Documentation for the functions from this example can be found in |
| 77 | +API docs (the api/ directory in GitHub's repository): |
| 78 | + |
| 79 | +* [ExceptHook](../../../api/cefpython.md#excepthook) |
| 80 | +* [Initialize()](../../../api/cefpython.md#initialize) |
| 81 | +* [CreateBrowserSync()](../../../api/cefpython.md#createbrowsersync) |
| 82 | +* [Browser](../../../api/Browser.md) object |
| 83 | +* [MessageLoop()](../../../api/cefpython.md#messageloop) |
| 84 | +* [Shutdown()](../../../api/cefpython.md#shutdown) |
| 85 | + |
| 86 | + |
| 87 | +## CEF's multiprocess architecture |
| 88 | + |
| 89 | +... |
| 90 | + |
| 91 | + |
| 92 | +## Handling Python exceptions |
| 93 | + |
| 94 | +... |
0 commit comments