CEF 即 (Chromium Embedded Framework);cef 是优秀的 WebView 组件。
pip install wxpython==4.2
wxPython-4.2.0-cp37-cp37m-win_amd64.whl (18.0 MB)
Successfully installed wxpython-4.2.0
pip install cefpython3
cefpython3-66.1-py2.py3-none-win_amd64.whl (69.0 MB)
Successfully installed cefpython3-66.1
cd \Python37\Lib\site-packages\cefpython3\examples
copy wxpython.py wx_cef.py
用的图片在 \Python37\Lib\site-packages\cefpython3\examples\resources\
编写 wx_cef.py 如下
# -*- coding: utf-8 -*-
# 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 os
import sys
import platform
import wx
from cefpython3 import cefpython as cef
import win32com.client # TTS
sapi = win32com.client.Dispatch("SAPI.SpVoice")
#help(cef.PyBrowser)
# Platforms
WINDOWS = (platform.system() == "Windows")
LINUX = (platform.system() == "Linux")
MAC = (platform.system() == "Darwin")
# Configuration
WIDTH = 900
HEIGHT = 600
# Globals
g_count_windows = 0
baseurl = "http://localhost:8888/"
def main():
check_versions()
# To shutdown all CEF processes on error
sys.excepthook = cef.ExceptHook
settings = {}
if WINDOWS:
# noinspection PyUnresolvedReferences, PyArgumentList
cef.DpiAware.EnableHighDpiSupport()
cef.Initialize(settings=settings)
app = CefApp(False)
app.MainLoop()
del app # Must destroy before calling Shutdown
if not MAC:
# On Mac shutdown is called in OnClose
cef.Shutdown()
def check_versions():
print("[wx_cef.py] CEF Python {ver}".format(ver=cef.__version__))
print("[wx_cef.py] Python {ver} {arch}".format(
ver=platform.python_version(), arch=platform.architecture()[0]))
print("[wx_cef.py] wxPython {ver}".format(ver=wx.version()))
# CEF Python version requirement
assert 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 be
called on all operating systems, but scales only for Windows.
If scaled value is bigger than the work area on the display
then 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_width
if height > max_height:
height = max_height
return width,

文章介绍了如何使用CEF(ChromiumEmbeddedFramework)和wxPython在Python中创建一个带有菜单和浏览器控件的应用,展示了版本检查、窗口调整、事件处理和浏览器嵌入等关键步骤。
最低0.47元/天 解锁文章
8812

被折叠的 条评论
为什么被折叠?



