2

I'm trying to load an Html file with wx.html.HtmlWindow. The Html file is linked to an external style sheet (CSS) file. The problem is the Html window only shows the plain Html file without styling (colors, fonts, etc.) How can I solve this issue?

HTML code:

<html>
<head>
<title>Embedded Style Sample</title>
<link href="E:\pythonGUI\styles.css" rel="stylesheet" type="text/css"/></head>
<body>
<h1>Embedded Style Sample testing</h1>
<h2>Next Line</h2>
</body>
</html>

CSS code:

h1{
    color: #0000FF;
  }
  h2{
    color: #00CCFF;
  }

浏览器上的 Html 文件

Python code:

import  wx 
import  wx.html
import  wx.html2 
  
class MyHtmlFrame(wx.Frame):
    def __init__(self, parent, title): 
        wx.Frame.__init__(
            self, 
            parent, 
            -1, 
            title, 
            size = (600,400)
        )
        html = wx.html.HtmlWindow(self) 
        html.LoadPage("E:\\pythonGUI\\newtest.html") 

app = wx.App()  
frm = MyHtmlFrame(None, "Simple HTML File Viewer")  
frm.Show()  
app.MainLoop()

the Html file show on HTML window:

在 HTML 窗口中显示的 Html 文件

4

2 回答 2

1

If you want complete HTML/CSS support as well as a Javascript engine, consider using wx.html2.WebView instead.

From wxpython docs:

wx.html doesn’t really have CSS support but it does support a few simple styles: you can use "text-align", "width", "vertical-align" and "background" with all elements and for SPAN elements a few other styles are additionally recognized:

color

font-family

font-size (only in point units)

font-style (only “oblique”, “italic” and “normal” values are supported)

font-weight (only “bold” and “normal” values are supported)

text-decoration (only “underline” value is supported)
于 2020-10-13T08:59:51.890 回答
0

使用 wx.html2.WebView 而不是 wx.html.HtmlWindow:

import wx
import wx.html2

class About(wx.Frame):
    def __init__(self):
        wx.Panel.__init__(self,None,-1,title="Title",size=(700,700))

class Test(wx.Frame):
    def __init__(self,title,pos,size):
        wx.Frame.__init__(self,None,-1,title,pos,size)
        self.tester=wx.html2.WebView.New(self)
        self.tester.LoadURL("E:\\pythonGUI\\newtest.html")

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = Test("html2 web view", (20, 20), (800, 600))
    frame.Show()
    app.MainLoop()
于 2020-10-13T10:11:25.180 回答