# An example of embedding CEF browser in a PyQt4 application.
# Tested with PyQt "4.9.1".
# Command for installing PyQt4: "sudo apt-get install python-qt4".
# The official CEF Python binaries come with tcmalloc hook
# disabled. But if you've built custom binaries and kept tcmalloc
# hook enabled, then be aware that in such case it is required
# for the cefpython module to be the very first import in
# python scripts. See Issue 73 in the CEF Python Issue Tracker
# for more details.
import ctypes, os, sys
libcef_so = os.path.join(os.path.dirname(os.path.abspath(__file__)),\
'libcef.so')
if os.path.exists(libcef_so):
# Import local module
ctypes.CDLL(libcef_so, ctypes.RTLD_GLOBAL)
if 0x02070000 hasPendingEvents()
# returns False.
# But there is a bug in Qt, hasPendingEvents() returns
# always true.
# (The behavior described above was tested on Windows
# with pyqt 4.8, maybe this is not true anymore,
# test it TODO)
cefpython.MessageLoopWork()
def stopTimer(self):
# Stop the timer after Qt message loop ended, calls to
# MessageLoopWork() should not happen anymore.
self.timer.stop()
if __name__ == '__main__':
print("[pyqt.py] PyQt version: %s" % QtCore.PYQT_VERSION_STR)
print("[pyqt.py] QtCore version: %s" % QtCore.qVersion())
# Intercept python exceptions. Exit app immediately when exception
# happens on any of the threads.
sys.excepthook = ExceptHook
# Application settings
settings = {
"debug": True, # cefpython debug messages in console and in log_file
"log_severity": cefpython.LOGSEVERITY_INFO, # LOGSEVERITY_VERBOSE
"log_file": GetApplicationPath("debug.log"), # Set to "" to disable.
# This directories must be set on Linux
"locales_dir_path": cefpython.GetModuleDirectory()+"/locales",
"resources_dir_path": cefpython.GetModuleDirectory(),
"browser_subprocess_path": "%s/%s" % (
cefpython.GetModuleDirectory(), "subprocess"),
}
# Command line switches set programmatically
switches = {
# "proxy-server": "socks5://127.0.0.1:8888",
# "enable-media-stream": "",
# "--invalid-switch": "" -> Invalid switch name
}
cefpython.Initialize(settings, switches)
app = CefApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
app.exec_()
app.stopTimer()
# Need to destroy QApplication(), otherwise Shutdown() fails.
# Unset main window also just to be safe.
del mainWindow
del app
cefpython.Shutdown()