当前位置: 首页 > news >正文

wxpython:wx.html2 是好用的 WebView 组件

wxpython : wx.html2 是好用的 WebView 组件。

pip install wxpython==4.2
wxPython-4.2.0-cp37-cp37m-win_amd64.whl (18.0 MB)
Successfully installed wxpython-4.2.0

cd \Python37\Scripts

wxdemo.exe 取得 wxPython-demo-4.2.0.tar.gz

wxdocs.exe 取得 wxPython-docs-4.2.0.tar.gz

编写 test_wx_html2.py 如下

# -*- coding: utf-8 -*-
import os
import sys
import wx
import wx.html2 as wbv# WebView Backends
backends = [(wbv.WebViewBackendEdge, 'WebViewBackendEdge'),(wbv.WebViewBackendIE, 'WebViewBackendIE'),(wbv.WebViewBackendWebKit, 'WebViewBackendWebKit'),(wbv.WebViewBackendDefault, 'WebViewBackendDefault'),
]#----------------------------------------------------------------------
class TestPanel(wx.Panel):def __init__(self, parent, id, frame=None):wx.Panel.__init__(self, parent, -1)self.current = "http://localhost:8888"self.frame = frameif frame:self.titleBase = frame.GetTitle()sizer = wx.BoxSizer(wx.VERTICAL)btnSizer = wx.BoxSizer(wx.HORIZONTAL)# The Internet Explorer emulation level is a persistent per-user and# per-application value.wbv.WebView.MSWSetEmulationLevel(wbv.WEBVIEWIE_EMU_IE11)# If you would like to reset it back to the default (no-emulation) then# it can be done by calling this:# wbv.WebView.MSWSetEmulationLevel(wbv.WEBVIEWIE_EMU_DEFAULT)# Find an available backendbackend = Nonefor id, name in backends:available = wbv.WebView.IsBackendAvailable(id)print("Backend 'wx.html2.{}' availability: {}\n".format(name, available))if available and backend is None:backend = idprint("Using backend: '{}'\n".format(str(backend, 'ascii')))# Create the WebViewself.wv = wbv.WebView.New(self, backend=backend)self.Bind(wbv.EVT_WEBVIEW_NAVIGATING, self.OnWebViewNavigating, self.wv)self.Bind(wbv.EVT_WEBVIEW_LOADED, self.OnWebViewLoaded, self.wv)btn = wx.Button(self, -1, "Open", style=wx.BU_EXACTFIT)self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)btn = wx.Button(self, -1, "<--", style=wx.BU_EXACTFIT)self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn)btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)self.Bind(wx.EVT_UPDATE_UI, self.OnCheckCanGoBack, btn)btn = wx.Button(self, -1, "-->", style=wx.BU_EXACTFIT)self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn)btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)self.Bind(wx.EVT_UPDATE_UI, self.OnCheckCanGoForward, btn)btn = wx.Button(self, -1, "Stop", style=wx.BU_EXACTFIT)self.Bind(wx.EVT_BUTTON, self.OnStopButton, btn)btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)btn = wx.Button(self, -1, "Refresh", style=wx.BU_EXACTFIT)self.Bind(wx.EVT_BUTTON, self.OnRefreshPageButton, btn)btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)txt = wx.StaticText(self, -1, "Location:")btnSizer.Add(txt, 0, wx.CENTER|wx.ALL, 2)self.location = wx.ComboBox(self, -1, "", style=wx.CB_DROPDOWN|wx.TE_PROCESS_ENTER)self.location.AppendItems(['https://fanyi.baidu.com/','https://cn.bing.com/','https://fanyi.sogou.com/'])self.Bind(wx.EVT_COMBOBOX, self.OnLocationSelect, self.location)self.location.Bind(wx.EVT_TEXT_ENTER, self.OnLocationEnter)btnSizer.Add(self.location, 1, wx.EXPAND|wx.ALL, 2)sizer.Add(btnSizer, 0, wx.EXPAND)sizer.Add(self.wv, 1, wx.EXPAND)self.SetSizer(sizer)self.wv.LoadURL(self.current)def ShutdownDemo(self):# put the frame title backif self.frame:self.frame.SetTitle(self.titleBase)# WebView eventsdef OnWebViewNavigating(self, evt):# this event happens prior to trying to get a resourceif evt.GetURL() == 'http://www.microsoft.com/':if wx.MessageBox("Are you sure you want to visit Microsoft?",style=wx.YES_NO|wx.ICON_QUESTION) == wx.NO:# This is how you can cancel loading a page.evt.Veto()def OnWebViewLoaded(self, evt):# The full document has loadedself.current = evt.GetURL()self.location.SetValue(self.current)# Control bar eventsdef OnLocationSelect(self, evt):url = self.location.GetStringSelection()print('OnLocationSelect: %s\n' % url)self.wv.LoadURL(url)def OnLocationEnter(self, evt):url = self.location.GetValue()self.location.Append(url)self.wv.LoadURL(url)def OnOpenButton(self, event):dlg = wx.TextEntryDialog(self, "Open Location","Enter a full URL or local path",self.current, wx.OK|wx.CANCEL)dlg.CentreOnParent()if dlg.ShowModal() == wx.ID_OK:self.current = dlg.GetValue()self.wv.LoadURL(self.current)dlg.Destroy()def OnPrevPageButton(self, event):self.wv.GoBack()def OnNextPageButton(self, event):self.wv.GoForward()def OnCheckCanGoBack(self, event):event.Enable(self.wv.CanGoBack())def OnCheckCanGoForward(self, event):event.Enable(self.wv.CanGoForward())def OnStopButton(self, evt):self.wv.Stop()def OnRefreshPageButton(self, evt):self.wv.Reload()class MyFrame(wx.Frame):    def __init__(self):super().__init__(parent=None, title='wx.html2 示例', size=(1000, 600))self.panel = TestPanel(self, wx.ID_ANY)self.Show(True)if __name__ == '__main__':app = wx.App(False) # False: 意味着不重定向标准输出和错误输出到窗口上frame = MyFrame()app.MainLoop()

运行 python test_wx_html2.py 

参考: wxpython事件绑定方法的选择和应用

http://www.lryc.cn/news/141557.html

相关文章:

  • 《QT+PCL 第五章》点云特征-PFH
  • 【分享】小型园区组网场景
  • LeetCode 1267. 统计参与通信的服务器
  • 169. 多数元素(哈希表)
  • 微服务集成spring cloud sentinel
  • 2023年最新版Windows环境下|Java8(jdk1.8)安装教程
  • linux -- jdk 的安装
  • 网络安全—黑客技术(学习笔记)
  • Java入职第十一天,深入了解静态代理和动态代理(jdk、cglib)
  • Snappy算法:高速压缩和解压缩技术的顶尖玩家
  • Python中的format()函数详细讲解
  • 11. 盛最多水的容器(c++题解)
  • 历史最佳二季度表现后,爱奇艺想为用户提供更多价值
  • HDLBits-Verilog学习记录 | Verilog Language-Basics(2)
  • Ubuntu22.0网络/网卡丢失
  • Linux 常用
  • AWS 提示证书签名过期无法自动更新
  • Git版本管理(01) 简介 基本提交相关命令
  • 解决 vue项目报错:digital envelope routines::unsupported
  • 【Java基础增强】类加载器和反射
  • 【Java】数据类型变量
  • 护目镜佩戴检测识别算法
  • NOIOLPJ2022B. 数学游戏 分析
  • android studio gradle build running慢 卡住不动 失败 原因与解决方式
  • 如何保障Facebook账号登录稳定
  • 当前目录下的excel文件的两列内容的相似度比较
  • Cookie for Mac:隐私保护工具保护您的在线隐私
  • Huggingface训练Transformer
  • IA-YOLO项目中DIP模块的初级解读
  • MathType7.4mac最新版本数学公式编辑器安装教程