0
  1. 我将条件表达式放在 {2}、{3} 中,并尝试在 Ture 的情况下使用绿色,在 False 的情况下使用红色。是否可以使用 SetForegroundColour 来表示颜色?如果你运行它,它将是无。你需要另一种方法吗?

  2. 我还尝试background-image: url ('1.png');指定背景,它是白色背景。在body标签内部使用时<img scr = 1.png />,出现x显示图片错误。

- - 编码:utf-8 - -

导入 wx

导入 wx.html2

导入 sqlite3

class AppFrame(wx.Frame):    
    def __init__(self, parent):    
        super(AppFrame, self).__init__(parent)    
        self.html_view = wx.html2.WebView.New(self)    
        sizer = wx.BoxSizer(wx.VERTICAL)    
        sizer.Add(self.html_view, 1, wx.EXPAND)    
        self.SetSizer(sizer)    

if __name__ == '__main__':    
    i = 'test1'    
    j = 'test2'    
    k = ('True', 'False')    
    l = ('True(color Green)', 'False(color Red)', 'How??')    

    for loop1 in k: pass    
    if loop1 == 'True': a = print(k[0])    
    else: a = print(k[1])    

    app = wx.App()    
    frame = AppFrame(None)    
    frame.html_view.SetPage("""    
<!doctype html>    
<html lang="ko">    
  <head>    
    <meta charset="utf-8">    
    <title>HTML TEST</title>    
    <style>    
      table{{    
        width: 100%;    
        border-collapse: collapse    
      }}    
      table, th, td{{    
        border: 2px solid #bcbcbc;    
        text-align: center;    
      }}    
      body{{    
        background-image:url('1.png');    
        background-position: conter conter;    
        background-color:gold;    
      }}    
    </style>    
  </head>    
  <body>    
    <table>    
      <caption><h1>Hello world!<h1></caption>    
      <thead>    
        <tr>          
          <th>{0}</th>    
          <th>{1}</th>    
          <th>Ipsum</th>    
          <th>Ipsum</th>    
          <th>Ipsum</th>    
          <th>Ipsum</th>      
        </tr>    
      </thead>    
      <tbody>    
        <tr>          
          <td>{2}</td>    
          <td>{3}</td>    
          <td>Dolor</td>    
          <td>Dolor</td>    
          <td>Dolor</td>    
          <td>Dolor</td>      
        </tr>    
      </tbody>    
      <tfoot>    
        <tr>    
          <td colspan="6">Table Foot</td>    
        </tr>    
      </tfoot>      
    </table>    
    <script>    
        if({0} == 'True'){{    
            ???    
        }}      
        else{{    
            ???    
        }}      
    </script>    
  </body>    
</html>    
""".format(i, j, a, l), "")    
    frame.Show(True)    
    app.MainLoop()

在此处输入图像描述

4

1 回答 1

1

有点难以理解你想要什么,但这是使用.format. 建议修改 Python 3 的示例。CSS 片段background-color可用于任何支持它的元素,例如您的<th></th>元素。如果要更改文本颜色,只需使用color. 同样可以应用于您<style>的 HTML 代码部分。

import wx
import wx.html2
class AppFrame(wx.Frame):
    def __init__(self, parent):
        super(AppFrame, self).__init__(parent)
        self.html_view = wx.html2.WebView.New(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.html_view, 1, wx.EXPAND)
        self.SetSizer(sizer)

if __name__ == '__main__':

    Test = True

    if Test == True:
        color = 'Green'
    else:
        color = 'Red'

    app = wx.App()
    frame = AppFrame(None)
    frame.html_view.SetPage("""
            <!doctype html>
            <html lang="ko">
              <head>
                <meta charset="utf-8">
                <title>HTML TEST</title>
                <style>
                  body{{
                    background-position: conter conter;
                    background-color:gold;
                  }}
                </style>
              </head>
              <body>
              <p>Normal text</p>
              </br>
              <p style="background-color:{0}">Conditionally changed background for Test={1}</p>
              </br>
              </body>
            </html>""".format(color, Test), "")
    frame.Show(True)
    app.MainLoop()

有条件地改变背景 Test=True

于 2017-08-28T10:02:45.473 回答