1- # CEF Python 3 example application.
1+ # Example of embedding CEF browser using the PyWin32 extension.
2+ # Tested with pywin32 version 218.
23
3- # Checking whether python architecture and version are valid, otherwise an obfuscated error
4- # will be thrown when trying to load cefpython.pyd with a message "DLL load failed".
54import platform
65if platform .architecture ()[0 ] != "32bit" :
7- raise Exception ("Architecture not supported: %s" % platform .architecture ()[0 ])
6+ raise Exception ("Architecture not supported: %s" \
7+ % platform .architecture ()[0 ])
88
99import os , sys
1010libcef_dll = os .path .join (os .path .dirname (os .path .abspath (__file__ )),
1111 'libcef.dll' )
1212if os .path .exists (libcef_dll ):
13- # Import the local module.
13+ # Import a local module
1414 if 0x02070000 <= sys .hexversion < 0x03000000 :
1515 import cefpython_py27 as cefpython
1616 elif 0x03000000 <= sys .hexversion < 0x04000000 :
1717 import cefpython_py32 as cefpython
1818 else :
1919 raise Exception ("Unsupported python version: %s" % sys .version )
2020else :
21- # Import the package.
21+ # Import an installed package
2222 from cefpython3 import cefpython
2323
2424import cefwindow
2525import win32con
2626import win32gui
27+ import win32api
2728import time
2829
2930DEBUG = True
3031
32+ # -----------------------------------------------------------------------------
33+ # Helper functions.
34+
35+ def Log (msg ):
36+ print ("[pywin32.py] %s" % str (msg ))
37+
3138def GetApplicationPath (file = None ):
32- import re , os
39+ import re , os , platform
3340 # If file is None return current directory without trailing slash.
3441 if file is None :
3542 file = ""
@@ -43,7 +50,8 @@ def GetApplicationPath(file=None):
4350 else :
4451 path = os .getcwd ()
4552 path = path + os .sep + file
46- path = re .sub (r"[/\\]+" , re .escape (os .sep ), path )
53+ if platform .system () == "Windows" :
54+ path = re .sub (r"[/\\]+" , re .escape (os .sep ), path )
4755 path = re .sub (r"[/\\]+$" , "" , path )
4856 return path
4957 return str (file )
@@ -52,7 +60,7 @@ def ExceptHook(excType, excValue, traceObject):
5260 import traceback , os , time , codecs
5361 # This hook does the following: in case of exception write it to
5462 # the "error.log" file, display it to the console, shutdown CEF
55- # and exit application immediately by ignoring "finally" (_exit()).
63+ # and exit application immediately by ignoring "finally" (os. _exit()).
5664 errorMsg = "\n " .join (traceback .format_exception (excType , excValue ,
5765 traceObject ))
5866 errorFile = GetApplicationPath ("error.log" )
@@ -67,7 +75,7 @@ def ExceptHook(excType, excValue, traceObject):
6775 fp .write ("\n [%s] %s\n " % (
6876 time .strftime ("%Y-%m-%d %H:%M:%S" ), errorMsg ))
6977 except :
70- print ("cefpython: WARNING: failed writing to error file: %s" % (
78+ print ("[pywin32.py] WARNING: failed writing to error file: %s" % (
7179 errorFile ))
7280 # Convert error message to ascii before printing, otherwise
7381 # you may get error like this:
@@ -79,6 +87,8 @@ def ExceptHook(excType, excValue, traceObject):
7987 cefpython .Shutdown ()
8088 os ._exit (1 )
8189
90+ # -----------------------------------------------------------------------------
91+
8292def CefAdvanced ():
8393 sys .excepthook = ExceptHook
8494
@@ -106,9 +116,14 @@ def CefAdvanced():
106116 browserSettings ["universal_access_from_file_urls_allowed" ] = True
107117 browserSettings ["file_access_from_file_urls_allowed" ] = True
108118
109- windowHandle = cefwindow .CreateWindow (title = "CEF Python 3 example" ,
110- className = "cefpython3_example" , width = 800 , height = 600 ,
111- icon = "icon.ico" , windowProc = wndproc )
119+ if os .path .exists ("icon.ico" ):
120+ icon = os .path .abspath ("icon.ico" )
121+ else :
122+ icon = ""
123+
124+ windowHandle = cefwindow .CreateWindow (title = "pywin32 example" ,
125+ className = "cefpython3_example" , width = 1024 , height = 768 ,
126+ icon = icon , windowProc = wndproc )
112127 windowInfo = cefpython .WindowInfo ()
113128 windowInfo .SetAsChild (windowHandle )
114129 browser = cefpython .CreateBrowserSync (windowInfo , browserSettings ,
@@ -125,5 +140,10 @@ def QuitApplication(windowHandle, message, wparam, lparam):
125140 win32gui .PostQuitMessage (0 )
126141 return 0
127142
143+ def GetPywin32Version ():
144+ fixed_file_info = win32api .GetFileVersionInfo (win32api .__file__ , '\\ ' )
145+ return fixed_file_info ['FileVersionLS' ] >> 16
146+
128147if __name__ == "__main__" :
148+ Log ("pywin32 version = %s" % GetPywin32Version ())
129149 CefAdvanced ()
0 commit comments