# Example of embedding CEF Python browser using wxPython library.# This example has a top menu and a browser widget without navigation bar.# Tested configurations:# - wxPython 4.0 on Windows/Mac/Linux# - wxPython 3.0 on Windows/Mac# - wxPython 2.8 on Linux# - CEF Python v66.0+import wxfrom cefpython3 import cefpython as cefimport platformimport sysimport os# PlatformsWINDOWS = (platform.system() == "Windows")LINUX = (platform.system() == "Linux")MAC = (platform.system() == "Darwin")if MAC:try:# noinspection PyUnresolvedReferencesfrom AppKit import NSAppexcept ImportError:print("[wxpython.py] Error: PyObjC package is missing, ""cannot fix Issue #371")print("[wxpython.py] To install PyObjC type: ""pip install -U pyobjc")sys.exit(1)# ConfigurationWIDTH = 900HEIGHT = 640# Globalsg_count_windows = 0def main():check_versions()sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on errorsettings = {}if MAC:# Issue #442 requires enabling message pump on Mac# and calling message loop work in a timer both at# the same time. This is an incorrect approach# and only a temporary fix.settings["external_message_pump"] = Trueif WINDOWS:# noinspection PyUnresolvedReferences, PyArgumentListcef.DpiAware.EnableHighDpiSupport()cef.Initialize(settings=settings)app = CefApp(False)app.MainLoop()del app # Must destroy before calling Shutdownif not MAC:# On Mac shutdown is called in OnClosecef.Shutdown()def check_versions():print("[wxpython.py] CEF Python {ver}".format(ver=cef.__version__))print("[wxpython.py] Python {ver} {arch}".format(ver=platform.python_version(), arch=platform.architecture()[0]))print("[wxpython.py] wxPython {ver}".format(ver=wx.version()))# CEF Python version requirementassert cef.__version__ >= "66.0", "CEF Python v66.0+ required to run this"def scale_window_size_for_high_dpi(width, height):"""Scale window size for high DPI devices. This func can becalled on all operating systems, but scales only for Windows.If scaled value is bigger than the work area on the displaythen it will be reduced."""if not WINDOWS:return width, height(_, _, max_width, max_height) = wx.GetClientDisplayRect().Get()# noinspection PyUnresolvedReferences(width, height) = cef.DpiAware.Scale((width, height))if width > max_width:width = max_widthif height > max_height:height = max_heightreturn width, heightclass MainFrame(wx.Frame):def __init__(self):self.browser = None# Must ignore X11 errors like 'BadWindow' and others by# installing X11 error handlers. This must be done after# wx was intialized.if LINUX:cef.WindowUtils.InstallX11ErrorHandlers()global g_count_windowsg_count_windows += 1if WINDOWS:# noinspection PyUnresolvedReferences, PyArgumentListprint("[wxpython.py] System DPI settings: %s"% str(cef.DpiAware.GetSystemDpi()))if hasattr(wx, "GetDisplayPPI"):print("[wxpython.py] wx.GetDisplayPPI = %s" % wx.GetDisplayPPI())print("[wxpython.py] wx.GetDisplaySize = %s" % wx.GetDisplaySize())print("[wxpython.py] MainFrame declared size: %s"% str((WIDTH, HEIGHT)))size = scale_window_size_for_high_dpi(WIDTH, HEIGHT)print("[wxpython.py] MainFrame DPI scaled size: %s" % str(size))wx.Frame.__init__(self, parent=None, id=wx.ID_ANY,title='wxPython example', size=size)# wxPython will set a smaller size when it is bigger# than desktop size.print("[wxpython.py] MainFrame actual size: %s" % self.GetSize())self.setup_icon()self.create_menu()self.Bind(wx.EVT_CLOSE, self.OnClose)# Set wx.WANTS_CHARS style for the keyboard to work.# This style also needs to be set for all parent controls.self.browser_panel = wx.Panel(self, style=wx.WANTS_CHARS)self.browser_panel.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)self.browser_panel.Bind(wx.EVT_SIZE, self.OnSize)if MAC:# Make the content view for the window have a layer.# This will make all sub-views have layers. This is# necessary to ensure correct layer ordering of all# child views and their layers. This fixes Window# glitchiness during initial loading on Mac (Issue #371).NSApp.windows()[0].contentView().setWantsLayer_(True)if LINUX:# On Linux must show before embedding browser, so that handle# is available (Issue #347).self.Show()# In wxPython 3.0 and wxPython 4.0 on Linux handle is# still not yet available, so must delay embedding browser# (Issue #349).if wx.version().startswith("3.") or wx.version().startswith("4."):wx.CallLater(100, self.embed_browser)else:# This works fine in wxPython 2.8 on Linuxself.embed_browser()else:self.embed_browser()self.Show()def setup_icon(self):icon_file = os.path.join(os.path.abspath(os.path.dirname(__file__)),"resources", "wxpython.png")# wx.IconFromBitmap is not available on Linux in wxPython 3.0/4.0if os.path.exists(icon_file) and hasattr(wx, "IconFromBitmap"):icon = wx.IconFromBitmap(wx.Bitmap(icon_file, wx.BITMAP_TYPE_PNG))self.SetIcon(icon)def create_menu(self):filemenu = wx.Menu()filemenu.Append(1, "Some option")filemenu.Append(2, "Another option")menubar = wx.MenuBar()menubar.Append(filemenu, "&File")self.SetMenuBar(menubar)def embed_browser(self):window_info = cef.WindowInfo()(width, height) = self.browser_panel.GetClientSize().Get()assert self.browser_panel.GetHandle(), "Window handle not available"window_info.SetAsChild(self.browser_panel.GetHandle(),[0, 0, width, height])self.browser = cef.CreateBrowserSync(window_info,url="https://www.google.com/")self.browser.SetClientHandler(FocusHandler())def OnSetFocus(self, _):if not self.browser:returnif WINDOWS:cef.WindowUtils.OnSetFocus(self.browser_panel.GetHandle(),0, 0, 0)self.browser.SetFocus(True)def OnSize(self, _):if not self.browser:returnif WINDOWS:cef.WindowUtils.OnSize(self.browser_panel.GetHandle(),0, 0, 0)elif LINUX:(x, y) = (0, 0)(width, height) = self.browser_panel.GetSize().Get()self.browser.SetBounds(x, y, width, height)self.browser.NotifyMoveOrResizeStarted()def OnClose(self, event):print("[wxpython.py] OnClose called")if not self.browser:# May already be closing, may be called multiple times on Macreturnif MAC:# On Mac things work differently, other steps are requiredself.browser.CloseBrowser()self.clear_browser_references()self.Destroy()global g_count_windowsg_count_windows -= 1if g_count_windows == 0:cef.Shutdown()wx.GetApp().ExitMainLoop()# Call _exit otherwise app exits with code 255 (Issue #162).# noinspection PyProtectedMemberos._exit(0)else:# Calling browser.CloseBrowser() and/or self.Destroy()# in OnClose may cause app crash on some paltforms in# some use cases, details in Issue #107.self.browser.ParentWindowWillClose()event.Skip()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.browser = Noneclass FocusHandler(object):def OnGotFocus(self, browser, **_):# Temporary fix for focus issues on Linux (Issue #284).if LINUX:print("[wxpython.py] FocusHandler.OnGotFocus:"" keyboard focus fix (Issue #284)")browser.SetFocus(True)class CefApp(wx.App):def __init__(self, redirect):self.timer = Noneself.timer_id = 1self.is_initialized = Falsesuper(CefApp, self).__init__(redirect=redirect)def OnPreInit(self):super(CefApp, self).OnPreInit()# On Mac with wxPython 4.0 the OnInit() event never gets# called. Doing wx window creation in OnPreInit() seems to# resolve the problem (Issue #350).if MAC and wx.version().startswith("4."):print("[wxpython.py] OnPreInit: initialize here"" (wxPython 4.0 fix)")self.initialize()def OnInit(self):self.initialize()return Truedef initialize(self):if self.is_initialized:returnself.is_initialized = Trueself.create_timer()frame = MainFrame()self.SetTopWindow(frame)frame.Show()def create_timer(self):# See also "Making a render loop":# http://wiki.wxwidgets.org/Making_a_render_loop# Another way would be to use EVT_IDLE in MainFrame.self.timer = wx.Timer(self, self.timer_id)self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)self.timer.Start(10) # 10ms timerdef on_timer(self, _):cef.MessageLoopWork()def OnExit(self):self.timer.Stop()return 0if __name__ == '__main__':main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。