77# - wxPython 2.8 on Linux
88# - CEF Python v66.0+
99
10- import wx
1110from cefpython3 import cefpython as cef
11+ import os
1212import platform
13+ import subprocess
1314import sys
14- import os
15+ import webbrowser
16+ import wx
1517
1618# Platforms
1719WINDOWS = (platform .system () == "Windows" )
2931 "pip install -U pyobjc" )
3032 sys .exit (1 )
3133
34+ # DevTools port and url. This is needed to workaround keyboard issues
35+ # in DevTools popup on Windows (Issue #381).
36+ DEVTOOLS_PORT = 0 # By default a random port is generated.
37+ if WINDOWS :
38+ DEVTOOLS_PORT = 54008
39+ DEVTOOLS_URL = "http://127.0.0.1:{0}/" .format (DEVTOOLS_PORT )
40+
3241# Configuration
3342WIDTH = 900
3443HEIGHT = 640
@@ -41,6 +50,8 @@ def main():
4150 check_versions ()
4251 sys .excepthook = cef .ExceptHook # To shutdown all CEF processes on error
4352 settings = {}
53+ if DEVTOOLS_PORT :
54+ settings ["remote_debugging_port" ] = DEVTOOLS_PORT
4455 if MAC :
4556 # Issue #442 requires enabling message pump on Mac
4657 # and calling message loop work in a timer both at
@@ -176,6 +187,10 @@ def embed_browser(self):
176187 [0 , 0 , width , height ])
177188 self .browser = cef .CreateBrowserSync (window_info ,
178189 url = "https://www.google.com/" )
190+ if WINDOWS :
191+ # Override the Brower.ShowDevTools method to fix keyboard
192+ # problems on Windows (Issue #381).
193+ self .browser .SetClientHandler (DevToolsHandler ())
179194 self .browser .SetClientHandler (FocusHandler ())
180195
181196 def OnSetFocus (self , _ ):
@@ -231,6 +246,29 @@ def clear_browser_references(self):
231246 self .browser = None
232247
233248
249+ class DevToolsHandler (object ):
250+ """This handler is set only on Windows platform."""
251+
252+ def ShowDevTools (self , browser , ** _ ):
253+ # Check if app was frozen with e.g. pyinstaller.
254+ if getattr (sys , "frozen" , None ):
255+ dir = os .path .dirname (os .path .realpath (__file__ ))
256+ executable = os .path .join (dir , "devtools.exe" );
257+ if os .path .exists (executable ):
258+ # If making executable with pyinstaller then create
259+ # executable for the devtools.py script as well.
260+ subprocess .Popen ([executable , DEVTOOLS_URL ])
261+ else :
262+ # Another way to show DevTools is to open it in Google Chrome
263+ # system browser.
264+ webbrowser .open (DEVTOOLS_URL )
265+ else :
266+ # Use the devtools.py script to open DevTools popup.
267+ dir = os .path .dirname (os .path .realpath (__file__ ))
268+ script = os .path .join (dir , "devtools.py" )
269+ subprocess .Popen ([sys .executable , script , DEVTOOLS_URL ])
270+
271+
234272class FocusHandler (object ):
235273 def OnGotFocus (self , browser , ** _ ):
236274 # Temporary fix for focus issues on Linux (Issue #284).
0 commit comments