wxpython + cef 是优秀的 WebView 组件

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# Platforms
WINDOWS = (platform.system() == "Windows")
LINUX = (platform.system() == "Linux")
MAC = (platform.system() == "Darwin")# Configuration
WIDTH = 1000
HEIGHT = 600# Globals
g_count_windows = 0
baseurl = "http://localhost:8888/"def 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: pass# noinspection PyUnresolvedReferences, PyArgumentList#cef.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("[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 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("[wx_cef.py] System DPI settings: %s"% str(cef.DpiAware.GetSystemDpi()))if hasattr(wx, "GetDisplayPPI"):print("[wx_cef.py] wx.GetDisplayPPI = %s" % wx.GetDisplayPPI())print("[wx_cef.py] wx.GetDisplaySize = %s" % wx.GetDisplaySize())print("[wx_cef.py] MainFrame declared size: %s"% str((WIDTH, HEIGHT)))size = scale_window_size_for_high_dpi(WIDTH, HEIGHT)print("[wx_cef.py] MainFrame DPI scaled size: %s" % str(size))wx.Frame.__init__(self, parent=None, id=wx.ID_ANY,title='wxPython + cef', size=size)# wxPython will set a smaller size when it is bigger# than desktop size.print("[wx_cef.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, "Open Url")filemenu.Append(2, "Go Back")menubar = wx.MenuBar()menubar.Append(filemenu, "&File")self.SetMenuBar(menubar)self.Bind(wx.EVT_MENU, self.OnOpenUrl, id=1)self.Bind(wx.EVT_MENU, self.OnGoBack, id=2)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=baseurl)self.browser.SetClientHandler(FocusHandler())def OnOpenUrl(self, event):dlg = wx.TextEntryDialog(self, "Open Location","Enter a full URL or local path","http://", wx.OK|wx.CANCEL)dlg.CentreOnParent()global baseurlif dlg.ShowModal() == wx.ID_OK:baseurl = dlg.GetValue()self.browser.LoadUrl(baseurl)dlg.Destroy()def OnGoBack(self, event):if self.browser:self.browser.GoBack()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("[wx_cef.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("[wx_cef.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("[wx_cef.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(True)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()

运行 python wx_cef.py

我注释了 #cef.DpiAware.EnableHighDpiSupport() ,以避免cef 刷新不完善时造成显示重影。

web 服务程序参见: python:mdict + bottle = web 查询英汉词典

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/90188.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

RK3399平台开发系列讲解(入门篇)内核模块详细加载/卸载过程

🚀返回专栏总目录 文章目录 一、 ko文件的文件格式二、内核模块加载过程三、内核模块卸载过程沉淀、分享、成长,让自己和他人都能有所收获!😄 📢 本篇将介绍内核模块详细加载/卸载过程。 一、 ko文件的文件格式 “KO” 文件通常是指 Linux 内核模块文件,这些文件包含…

jmeter 性能测试工具的使用(Web性能测试)

1、下载 该软件不用安装,直接解压打开即可使用。 2、使用 这里就在win下进行,图形界面较为方便   在目录apache-jmeter-2.13\bin 下可以见到一个jmeter.bat文件,双击此文件,即看到JMeter控制面板。主界面如下: 3、创…

【c语言】输出n行按如下规律排列的数

题述:输出n行按如下规律排列的数 输入: 4(应该指的是n) 输出: 思路: 利用下标的规律求解,考察数组下标的灵活应用,我们可以看出数从1开始是斜着往下放的,那么我们如何利用两层for循环求解这道题&#xff…

使用Python构建网络爬虫:提取网页内容和图片资源

网络爬虫是一种自动获取网页内容的程序,它可以帮助我们高效地收集网络上的有价值信息。本文将介绍如何使用Python构建网络爬虫,提取网页内容和图片资源。   一、环境准备   1.安装Python环境   首先,确保您已经安装了Python环境。访问P…

多张图片转为pdf怎么弄?

多张图片转为pdf怎么弄?在网络传输过程中,为了避免图片格式文件出现差错,并确保图片的清晰度和色彩不因不同设备而有所改变,常见的做法是将图片转换为PDF格式。然而,当涉及到多张图片时,逐一转换将会变得相…

7.Redis-list

list list常用命令lpushlrangelpushxrpushrpushxlpop / rpoplindexlinsertllenlremltrimlset 阻塞版本命令blpop/brpop 总结内部编码应用场景使用redis作为消息队列 redis中的 list 是一个双端队列, list 相当于是数组或者顺序表。list 并非是一个简单的数组,而是更…

数据驱动的生活:探索未来七天生活指数API的应用

前言 随着科技的不断发展,数据已经成为我们生活中不可或缺的一部分。从社交媒体上的点赞和分享,到电子邮件和搜索引擎的历史记录,数据正在以前所未有的速度积累。而这些数据的利用不仅仅停留在社交媒体或商业领域,它们还可以为我…

natApp内网穿透工作原理

如图所示,用户启动内网穿透工具会将token传入natapp服务器与我们自己的主机建立一个类似于websocket的长链接,当从外网访问我们主机的接口时,会进行一个本地接口地址的截取,然后进行拼接成我们主机应用的真实地址。然后将数据返回…

利用 IDEA IDE 的轻量编辑模式快速查看和编辑工程外的文本文件

作为程序员, 我们都知道 IDE 的很好用的, 它的文本编辑器功能也非常的强大, 用起来非常便捷. 在长年累月的使用中, 我们也变得对其非常熟悉, 以致于使用起其它简单地轻量级的文本编辑器来, 比如什么记事本, Notepad, UltraEdit 等等呀, 觉得既不方便又不熟悉. 关键是很多的操作…

【Go 基础篇】Go语言结构体基本使用

在Go语言中,结构体是一种重要的数据类型,用于定义和组织一组不同类型的数据字段。结构体允许开发者创建自定义的复合数据类型,类似于其他编程语言中的类。本文将深入探讨Go语言中结构体的定义、初始化、嵌套、方法以及与其他语言的对比&#…

SQL Server 2019导入txt数据

1、选择导入数据 2、选择Flat file Source 选择文件,如果第一行不是列名,就不勾选。 3、下一步 可以看看数据是否是对的 4、下一步 选择SQL server Native Client 11,数据库选择导入进的库 输入连接数据库的名字和要导入的数据库 下一…

postgis数据库从一张表中过滤出一部分数据到新表中

你可以使用以下步骤在PostGIS数据库中过滤objectid<100的数据&#xff0c;并将其创建为新表&#xff1a;打开PostGIS数据库的终端或客户端工具&#xff08;如Psql&#xff09;。 选择你要过滤数据的表。假设表名为"original_table"&#xff0c;该表包含一个名为&q…