# Example of embedding CEF browser using PyQt4, PyQt5 and
# PySide libraries. This example has two widgets: a navigation
# bar and a browser.
#
# Tested configurations:
# - PyQt 5.8.2 on Windows/Linux/Mac
# - PyQt 4.11 (qt 4.8) on Windows/Linux
# - PySide 1.2 (qt 4.8) on Windows/Linux/Mac
# - CEF Python v55.4+
#
# Issues with PySide 1.2 on Mac:
# - Keyboard focus issues when switching between controls (Issue #284)
# - Mouse cursor never changes when hovering over links (Issue #311)
# - Sometimes process hangs when quitting app
from cefpython3 import cefpython as cef
import ctypes
import os
import platform
import sys
# GLOBALS
PYQT4 = False
PYQT5 = False
PYSIDE = False
if "pyqt4" in sys.argv:
PYQT4 = True
# noinspection PyUnresolvedReferences
from PyQt4.QtGui import *
# noinspection PyUnresolvedReferences
from PyQt4.QtCore import *
elif "pyqt5" in sys.argv:
PYQT5 = True
# noinspection PyUnresolvedReferences
from PyQt5.QtGui import *
# noinspection PyUnresolvedReferences
from PyQt5.QtCore import *
# noinspection PyUnresolvedReferences
from PyQt5.QtWidgets import *
elif "pyside" in sys.argv:
PYSIDE = True
# noinspection PyUnresolvedReferences
import PySide
# noinspection PyUnresolvedReferences
from PySide import QtCore
# noinspection PyUnresolvedReferences
from PySide.QtGui import *
# noinspection PyUnresolvedReferences
from PySide.QtCore import *
else:
print("USAGE:")
print(" qt.py pyqt4")
print(" qt.py pyqt5")
print(" qt.py pyside")
sys.exit(1)
# Fix for PyCharm hints warnings when using static methods
WindowUtils = cef.WindowUtils()
# Platforms
WINDOWS = (platform.system() == "Windows")
LINUX = (platform.system() == "Linux")
MAC = (platform.system() == "Darwin")
# Configuration
WIDTH = 800
HEIGHT = 600
# OS differences
CefWidgetParent = QWidget
if LINUX and (PYQT4 or PYSIDE):
# noinspection PyUnresolvedReferences
CefWidgetParent = QX11EmbedContainer
def main():
check_versions()
sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error
cef.Initialize()
app = CefApplication(sys.argv)
main_window = MainWindow()
main_window.show()
main_window.activateWindow()
main_window.raise_()
app.exec_()
app.stopTimer()
del main_window # Just to be safe, similarly to "del app"
del app # Must destroy app object before calling Shutdown
cef.Shutdown()
def check_versions():
print("[qt.py] CEF Python {ver}".format(ver=cef.__version__))
print("[qt.py] Python {ver} {arch}".format(
ver=platform.python_version(), arch=platform.architecture()[0]))
if PYQT4 or PYQT5:
print("[qt.py] PyQt {v1} (qt {v2})".format(
v1=PYQT_VERSION_STR, v2=qVersion()))
elif PYSIDE:
print("[qt.py] PySide {v1} (qt {v2})".format(
v1=PySide.__version__, v2=QtCore.__version__))
# CEF Python version requirement
assert cef.__version__ >= "55.4", "CEF Python v55.4+ required to run this"
class MainWindow(QMainWindow):
def __init__(self):
# noinspection PyArgumentList
super(MainWindow, self).__init__(None)
self.cef_widget = None
self.navigation_bar = None
if PYQT4:
self.setWindowTitle("PyQt4 example")
elif PYQT5:
self.setWindowTitle("PyQt5 example")
elif PYSIDE:
self.setWindowTitle("PySide example")
self.setFocusPolicy(Qt.StrongFocus)
self.setupLayout()
def setupLayout(self):
self.resize(WIDTH, HEIGHT)
self.cef_widget = CefWidget(self)
self.navigation_bar = NavigationBar(self.cef_widget)
layout = QGridLayout()
# noinspection PyArgumentList
layout.addWidget(self.navigation_bar, 0, 0)
# noinspection PyArgumentList
layout.addWidget(self.cef_widget, 1, 0)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
layout.setRowStretch(0, 0)
layout.setRowStretch(1, 1)
# noinspection PyArgumentList
frame = QFrame()
frame.setLayout(layout)
self.setCentralWidget(frame)
if PYQT5 and WINDOWS:
# On Windows with PyQt5 main window must be shown first
# before CEF browser is embedded, otherwise window is
# not resized and application hangs during resize.
self.show()
# Browser can be embedded only after layout was set up
self.cef_widget.embedBrowser()
if PYQT5 and LINUX:
# On Linux with PyQt5 the QX11EmbedContainer widget is
# no more available. An equivalent in Qt5 is to create
# a hidden window, embed CEF browser in it and then
# create a container for that hidden window and replace
# cef widget in the layout with the container.
# noinspection PyUnresolvedReferences, PyArgumentList
self.container = QWidget.createWindowContainer(
self.cef_widget.hidden_window, parent=self)
# noinspection PyArgumentList
layout.addWidget(self.container, 1, 0)
def closeEvent(self, event):
# Close browser (force=True) and free CEF reference
if self.cef_widget.browser:
self.cef_widget.browser.CloseBrowser(True)
self.clear_browser_references()
def clear_browser_references(self):
# Clear browser references that you keep anywhere in your
# code. All references must be cleared for CEF to shutdown cleanly.
self.cef_widget.browser = None
class CefWidget(CefWidgetParent):
def __init__(self, parent=None):
# noinspection PyArgumentList
super(CefWidget, self).__init__(parent)
self.parent = parent
self.browser = None
self.hidden_window = None # Required for PyQt5 on Linux
self.show()
def focusInEvent(self, event):
# This event seems to never get called on Linux, as CEF is
# stealing all focus due to Issue #284.
if self.browser:
if WINDOWS:
WindowUtils.OnSetFocus(self.getHandle(), 0, 0, 0)
self.browser.SetFocus(True)
def focusOutEvent(self, event):
# This event seems to never get called on Linux, as CEF is
# stealing all focus due to Issue #284.
if self.browser:
self.browser.SetFocus(False)
def embedBrowser(self):
if PYQT5 and LINUX:
# noinspection PyUnresolvedReferences
self.hidden_window = QWindow()
window_info = cef.WindowInfo()
rect = [0, 0, self.width(), self.height()]
window_info.SetAsChild(self.getHandle(), rect)
self.browser = cef.CreateBrowserSync(window_info,
url="https://www.google.com/")
self.browser.SetClientHandler(LoadHandler(self.parent.navigation_bar))
self.browser.SetClientHandler(FocusHandler(self))
def getHandle(self):
if self.hidden_window:
# PyQt5 on Linux
return int(self.hidden_window.winId())
try:
# PyQt4 and PyQt5
return int(self.winId())
except:
# PySide:
# | QWidget.winId() returns