背景:為何需要內嵌 HTML 視窗?
- 避免「針對最低共同規格開發」或「僅支援單一瀏覽器」的兩難
- 自帶渲染引擎,不依賴系統 IE/WebView,版本一致、部署簡單
- wxHTML 是 wxWidgets 內建的輕量 HTML 渲染元件,支援 HTML 3.2/4.0 子集、基本 CSS、圖片、表格
最簡瀏覽器:30 行 Python 代碼
from wxPython.wx import *
from wxPython.html import *
import os, sys
class MyHtmlWindow(wxHtmlWindow):
def __init__(self, parent, id, frame):
wxHtmlWindow.__init__(self, parent, id)
class MyHtmlPanel(wxPanel):
def __init__(self, parent, id, frame):
wxPanel.__init__(self, parent, -1)
self.html = MyHtmlWindow(self, -1, frame)
self.box = wxBoxSizer(wxVERTICAL)
self.box.Add(self.html, 1, wxGROW)
self.SetSizer(self.box)
self.SetAutoLayout(True)
class MyFrame(wxFrame):
def __init__(self, parent, ID, title):
wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, wxSize(600,750))
panel = MyHtmlPanel(self, -1, self)
class MyApp(wxApp):
def OnInit(self):
frame = MyFrame(NULL, -1, "Example Browser")
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(0)
app.MainLoop()
進階:載入頁面、工具列、歷史導航
class MyHtmlPanel(wxPanel):
def __init__(self, parent, id, frame):
wxPanel.__init__(self, parent, -1)
self.frame = frame
self.cwd = os.path.split(sys.argv[0])[0] or os.getcwd()
self.html = MyHtmlWindow(self, -1, frame)
# 主佈局
self.box = wxBoxSizer(wxVERTICAL)
self.box.Add(self.html, 1, wxGROW)
# 工具列
tb = wxBoxSizer(wxHORIZONTAL)
for label, id, handler in [
("Load File", 1202, self.OnLoadFile),
("Load Page", 1203, self.OnLoadPage),
("Back", 1204, self.OnBack),
("Forward", 1205, self.OnForward)
]:
btn = wxButton(self, id, label)
EVT_BUTTON(self, id, handler)
tb.Add(btn, 1, wxGROW | wxALL, 2)
self.box.Add(tb, 0, wxGROW)
self.SetSizer(self.box)
self.SetAutoLayout(True)
def OnLoadPage(self, event):
dlg = wxTextEntryDialog(self, 'Location:')
if dlg.ShowModal() == wxID_OK:
self.html.LoadPage(dlg.GetValue())
dlg.Destroy()
def OnLoadFile(self, event):
dlg = wxFileDialog(self, wildcard='*.htm*', style=wxOPEN)
if dlg.ShowModal():
self.html.LoadPage(dlg.GetPath())
dlg.Destroy()
def OnBack(self, event):
if not self.html.HistoryBack():
wxMessageBox("No more items in history!")
def OnForward(self, event):
if not self.html.HistoryForward():
wxMessageBox("No more items in history!")
加入選單、狀態列、圖片支援
class MyApp(wxApp):
def OnInit(self):
wxInitAllImageHandlers() # 關鍵:啟用內建圖片格式支援 (PNG/JPG/GIF...)
frame = MyFrame(NULL, -1, "Example Browser")
frame.Show(True)
self.SetTopWindow(frame)
return True
class MyFrame(wxFrame):
def __init__(self, parent, ID, title):
wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, wxSize(600,750))
panel = MyHtmlPanel(self, -1, self)
self.CreateStatusBar() # 狀態列
# 選單
mnu_file = wxMenu()
mnu_file.Append(101, "E&xit", "Exit the browser")
menuBar = wxMenuBar()
menuBar.Append(mnu_file, "&File")
self.SetMenuBar(menuBar)
EVT_MENU(self, 101, self.Exit)
def Exit(self, event):
self.Close(True)
class MyHtmlPanel(wxPanel):
def __init__(self, parent, id, frame):
# ... 前略 ...
self.html.SetRelatedFrame(frame, "%s") # 標題列同步
self.html.SetRelatedStatusBar(0) # 狀態列同步
# ... 後略 ...
關鍵類別與方法速查
| 類別/方法 | 用途 |
wxHtmlWindow | 核心渲染視窗 |
LoadPage(url) | 載入本地/遠端 HTML |
HistoryBack() / HistoryForward() | 前進/後退歷史 |
SetRelatedFrame(frame, format) | 連結框架標題更新(%s 為頁面標題) |
SetRelatedStatusBar(index) | 連結狀態列顯示連結 URL、載入進度 |
wxInitAllImageHandlers() | 註冊所有內建圖片格式處理器 |
限制與替代方案
- wxHTML 不支援完整 CSS2/3、JavaScript、現代 HTML5
- 需完整 Web 相容性請改用 wxWebView(封裝系統 WebView:Windows 上為 Edge/IE、Linux 上為 WebKitGTK、macOS 上為 WebKit)
- C++ 開發者可直接使用
wxHtmlWindow C++ API,邏輯相同