【问题标题】:How to load HTML file with external link to CSS file with wxPython?如何使用 wxPython 加载带有指向 CSS 文件的外部链接的 HTML 文件?
【发布时间】:2020-10-12 12:46:21
【问题描述】:

我正在尝试使用 wx.html.HtmlWindow 加载 Html 文件。 Html 文件链接到外部样式表 (CSS) 文件。 问题是 Html 窗口只显示没有样式(颜色、字体等)的普通 Html 文件 我该如何解决这个问题?

HTML 代码:

<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 代码:

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

Python 代码:

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()

在 HTML 窗口中显示的 Html 文件:

【问题讨论】:

    标签: python html css wxpython wxhtmlwindow


    【解决方案1】:

    如果您想要完整的 HTML/CSS 支持以及 Javascript 引擎,请考虑改用 wx.html2.WebView

    来自 wxpython 文档:

    wx.html 并没有真正的 CSS 支持,但它确实支持一些 简单样式:您可以使用“text-align”、“width”、“vertical-align”和 “背景”与所有元素和 SPAN 元素其他一些 样式被额外识别:

    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)
    

    【讨论】:

      【解决方案2】:

      使用 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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-03-14
        • 2018-05-25
        • 2017-09-19
        • 2021-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多